首先要在工程里加入Audio Toolbox framework这个库,然后在需要调用的文件里#import <AudioToolbox/AudioToolbox.h>
最后在需要播放提示音的地方写上
AudioServicesPlaySystemSound(1106);
注:括号中为系统声音的id,详见最下面的列表。
为了方便大家测试系统声音,我写了一个demo供大家使用下载。
另外,如果想用自己的音频文件创建系统声音来播放的同学可以参考如下代码。
//Get the filename of the sound file:
NSString *path = [NSString stringWithFormat:@"%@%@", [[NSBundle mainBundle] resourcePath], @"/jad0007a.wav"];
//declare a system sound
id SystemSoundID soundID;
//Get a URL for the sound file
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
//Use audio sevices to create the sound
AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
//Use audio services to play the sound
AudioServicesPlaySystemSound(soundID);
顺便附上系统提示音对应的列表
AudioServices
AudioServices is a group of C functions in AudioToolbox for playing short (≤30 seconds) sounds.
Predefined sounds
There are some predefined system sounds, for the system sound ID in the range 1000 to 2000 (decimal), as shown below (from 2.0 to 5.0 beta). The system sounds are all stored in /System/Library/Audio/UISounds/.
kSystemSoundID_Vibrate.
本文介绍如何在iOS应用中播放系统预定义的声音及自定义音频文件。通过加入AudioToolbox框架并使用AudioServicesPlaySystemSound函数,开发者可以轻松实现声音播放功能。此外,还提供了获取和播放自定义音频文件的方法。
1202

被折叠的 条评论
为什么被折叠?



