【iOS】音频的简单使用(AudioServicesPlaySystemSound音频服务)

本文介绍了一种在iOS应用中优化音效播放的方法,重点介绍了如何实现下拉刷新时播放音效且不影响其他音频,同时解决了wav文件转码的问题,并详细展示了如何利用AudioToolbox框架进行音效的加载和播放。

当前开发的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

转载于:https://my.oschina.net/onepieceios/blog/737364

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值