• 音频播放


    音频格式

    硬件解码

    软件解码

    AAC

    YES

    YES

    ALAC

    YES

    YES

    HE-AAC

    YES

     

    iLBC

     

    YES

    IMA4

     

    YES

    Linea PCM

     

    YES

    MP3

    YES

    YES

    μ-law and a-law

     

    YES

    CAF

    YES

    YES

    注意:硬件解码器一次只能对一个音频文件解码。在实际应用中通常使用非压缩的音频格式(AIFF)或者CAF音频格式,从而减低系统在音频解码上的消耗,达到省电的目的.

    • 转换aiff格式
      • afconvert -f AIFF -d I8 filename
    • 转换caf格式
      • afconvert -f caff -d aac -b 32000 filename
    • 批量转换
      • find . -name '*.mp3' -exec afconvert -f caff -d aac -b 32000 {} ;

    ------------------------------------------------------------------------------

    播放音效的工具类

    #import "ROAudioTool.h"

    #import <AVFoundation/AVFoundation.h>

    @implementation ROAudioTool

     /**  声明私有的成员变量 ***/

    static NSMutableDictionary *_soundIDs;

    /**

     *  保证soundIDs, 只创建1次

     */

    + (void)initialize

    {

        _soundIDs = [NSMutableDictionary dictionary];

    }

    //#warning /**  与initialize, 异曲同工, 但是调用 soundID的时候不能直接用 _soundID, 必须采用[self soundID], 否则直接调用静态变量 _soundID, 每次都创建一个 _soundID, 不会调用此方法***/

    //+ (NSMutableDictionary *)soundIDs

    //{

    //    if (!_soundIDs) {

    //        _soundIDs = [NSMutableDictionary dictionary];

    //    }

    //    return _soundIDs;

    //}

    + (void)playAudioWithFilename:(NSString *)filename

    {

        /*

        // 0. 创建URL

        NSURL *url = [[NSBundle mainBundle] URLForResource:filename withExtension:nil];

        

        // 1. 创建音效ID

        SystemSoundID outSystemSoundID;

        AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &outSystemSoundID);

        

        // 2. 播放本地音效

        // AudioServicesPlayAlertSound(nil)  ---- 既播放又振动

        // [注意] iOS8 模拟器不支持播放音效

        AudioServicesPlaySystemSound(outSystemSoundID);

         */

        

        // 1. 从字典中取soundID

        // 1.1 判断文件名是否为空

        if (filename == nil) return;

        SystemSoundID outSystemSoundID = [_soundIDs[filename] unsignedIntValue];

        

        // 1.2 判断soundID是否为空

        if (!outSystemSoundID) {

            LogRed(@"创建新的soundID");

            

            // 如果ID为空, 根据文件名创建url

            NSURL *url = [[NSBundle mainBundle] URLForResource:filename withExtension:nil];

            

            // 1.3 判断URL是否为nil

            if(url == nil) return;

            

            // 1.4 根据url创建soundID 并添加到字典中

            AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &outSystemSoundID);

            

            _soundIDs[filename] = @(outSystemSoundID);

        }

        

        // 2. 播放soundID

        AudioServicesPlaySystemSound(outSystemSoundID);

        

    }

    /**

     *  根据filename 销毁soundID

     */

    + (void)disposeAudioWithFilename:(NSString *)filename{

        if(filename == nil)return;

        

        // 1. 取出soundID

        SystemSoundID outSystemSoundID = [_soundIDs[filename] unsignedIntValue];

        

        if (outSystemSoundID) {

            // 2. 销毁soundID

            AudioServicesDisposeSystemSoundID(outSystemSoundID);

            

            // 3. 从字典中移除已经销毁的SoundID

            [_soundIDs removeObjectForKey:filename];

        }

    }

  • 相关阅读:
    日常排雷:redis报错 could not get a resource from the pool
    阿里云centos服务器tomcat启动后,浏览器请求无响应
    并发生产顺序单据号测试
    json 数据 格式,请求接口,部分字段无法注入
    baomidou 动态数据源@DS 使用问题
    SpringMVC框架深入(八)--SpringMVC原理
    Spring框架深入(七)--json数据交互
    框架理论深入(六)--拦截器
    Spring框架深入(五)--文件上传和异常处理
    int和Integer的区别
  • 原文地址:https://www.cnblogs.com/guangleijia/p/4836801.html
Copyright © 2020-2023  润新知