iOS音频播放(系统音效播放)
2016-06-28 本文已影响3735人
天蚕
昨天遇到了系统提示音的播放(类似于系统短音提示音,按键提示音),最终发现其实系统声音的播放其实很简单,在这里做一个纪录
这里的声音播放使用AVFoundation框架
播放系统音效(系统提示音效)
//其中的soundID是系统音效ID可以在下面地址找到
//soundID的类型是SystemSoundID其实就是UInt32需要强转一下eg:(UInt32)1007
AudioServicesPlaySystemSound(soundID);
系统音效ID查看:http://iphonedevwiki.net/index.php/AudioServices
播放网络缓存到沙盒document下的音频文件
//name是音频文件的名称
NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *urlString = [documents stringByAppendingPathComponent:name];
SystemSoundID soundID = 0;
if (!soundIDs)
{
soundIDs = [NSMutableDictionary dictionary];
}
else
{
soundID = (SystemSoundID)[soundIDs[soundIDName] integerValue];
}
if (soundID == 0)
{
NSURL *url = [NSURL URLWithString:urlString];
AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)url, &soundID);
soundIDs[soundIDName] = [NSString stringWithFormat:@"%zd",soundID];
}
AudioServicesPlaySystemSound(soundID);
播放网络app的boundle中的音频文件
//name是音频文件的名称
NSString *urlString = [NSString stringWithFormat:@"%@",[[NSBundle mainBundle] pathForResource:name ofType:nil]];
SystemSoundID soundID = 0;
if (!soundIDs)
{
soundIDs = [NSMutableDictionary dictionary];
}
else
{
soundID = (SystemSoundID)[soundIDs[soundIDName] integerValue];
}
if (soundID == 0)
{
NSURL *url = [NSURL URLWithString:urlString];
AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)url, &soundID);
soundIDs[soundIDName] = [NSString stringWithFormat:@"%zd",soundID];
}
AudioServicesPlaySystemSound(soundID);
为了方便使用将音频播放做了下简单封装成JLBAudioTool使用简单,有兴趣可以看一下https://github.com/tiancanfei/JLBAudioTool