当前开发的app,增加一个下拉刷新增加音效的功能,使用系统播放,不影响正在听歌等后台正在使用的app声音。另外,开发中,wav文件转码格式有问题,导致不能播放。
//播放系统声音需要导入框架:AudioToolbox.framework
//1.音频长度小于30秒
//2.格式只能是PCM或者IMA4
//3.文件必须被存储为.caf、.aif、或者.wav格式
//4.简单音频不能从内存播放,而只能是磁盘文件
- (void)play
{
/*测试播放系统音效*/
AudioServicesPlaySystemSound(1000);
}
- (void)soundPlay:(NSString*)name
{
[[SoundTools sharedTools] playSoundWithName:name];
}
//
// SoundTools.h
#import <Foundation/Foundation.h>
#import <AudioToolbox/AudioToolbox.h>
@interface SoundTools : NSObject
+ (instancetype)sharedTools;
//要播放的音效名
- (void)playSoundWithName:(NSString *)name;
@end
//
// SoundTools.m
#import "SoundTools.h"
@interface SoundTools()
{
NSDictionary * _soundDict;
}
@end
@implementation SoundTools
+ (instancetype)sharedTools
{
//Singleton instance
static SoundTools *manager;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = [self new];
});
return manager;
}
- (id)init
{
self = [super init];
if (self) {
// 完成所有音频文件的加载工作
_soundDict = [self loadSounds];
}
return self;
}
#pragma mark 加载指定的音频文件
- (SystemSoundID)loadSoundWithURL:(NSURL *)url
{
SystemSoundID soundID = 0;
OSStatus err = AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundID);
if (err) {
NSLog(@"Error occurred assigning system sound!");
return -1;
}
// AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundID);
// AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL, soundCompleteCallback, NULL);
return soundID;
}
#pragma mark 加载所有的音频文件
- (NSDictionary *)loadSounds
{
// 1. 取出bundle的路径名
NSString *mainBundlPath = [[NSBundle mainBundle] bundlePath];
NSString *bundlePath =[mainBundlPath stringByAppendingPathComponent:@"sound.bundle"];
// 2. 遍历目录
NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:bundlePath error:nil];
// 3. 遍历数组,创建SoundID,如何使用?
NSMutableDictionary *dictM = [NSMutableDictionary dictionaryWithCapacity:array.count];
[array enumerateObjectsUsingBlock:^(NSString *fileName, NSUInteger idx, BOOL *stop) {
// 1> 拼接URL
NSString *filePath = [bundlePath stringByAppendingPathComponent:fileName];
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
SystemSoundID soundID = [self loadSoundWithURL:fileURL];
// 将文件名作为键值
[dictM setObject:@(soundID) forKey:fileName];
}];
return dictM;
}
void soundCompleteCallback(SystemSoundID soundID,void * clientData){
AudioServicesDisposeSystemSoundID(soundID);
NSLog(@"播放完成...%@", @(soundID));
}
#pragma mark - 播放音频
- (void)playSoundWithName:(NSString *)name
{
SystemSoundID soundID = [_soundDict[name] unsignedIntValue];
NSLog(@"systemSounID %u",(unsigned int)soundID);
//断言它必须大于0;
NSAssert(soundID > 0, @"%@ 声音文件不存在!", name);
/*开始播放*/
AudioServicesPlaySystemSound(soundID);
#if 0
// 播放声音
// 同样遵守苹果的静音原则,如果用户静音,会震动!提示用户注意!
AudioServicesPlayAlertSound(soundID)
// 只播放声音,遵守苹果的静音原则 HIG
AudioServicesPlaySystemSound(soundID);
#endif
}
@end
参考链接 :
优化与封装 APP音效的播放 http://blog.youkuaiyun.com/ys410900345/article/details/43342195
iOS--音频的简单使用:http://www.jianshu.com/p/78afa5629cb5
系统声音System Sound ID 列表: http://my.oschina.net/are1OfBlog/blog/476751
AudioServicesPlaySystemSound: http://www.cnblogs.com/ruzhuan/p/3176715.html
声音视频iOS详解:http://www.cnblogs.com/kenshincui/p/4186022.html#music
后台播放音乐:http://www.cnblogs.com/qingche/p/4366335.html#3476359