zoukankan      html  css  js  c++  java
  • ios程序播放音频文件

    1.控制器代码

    #import "ViewController.h"

    #import <AVFoundation/AVFoundation.h>

    #import "RYAVTool.h"

     

    @interface ViewController ()

     

    @property(nonatomic,strong)NSMutableDictionary*allAvID;

     

    @end

     

    @implementation ViewController

     

    - (void)viewDidLoad {

        [super viewDidLoad];

        

    }

     

     

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

    {

       self.allAvID= [RYAVTool playAVWithName:@"m_06.wav"];

    }

    //内存警告时清理缓存

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        [self.allAvID enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {

            AudioServicesDisposeSystemSoundID([obj unsignedIntValue]);

        }];

        

        

    }

    @end

     

    2.辅助工具类代码

    #import <Foundation/Foundation.h>

     

    @interface RYAVTool : NSObject

     

    +(NSMutableDictionary*)playAVWithName:(NSString*)name;

     

    @end

     

    #import "RYAVTool.h"

    #import <AVFoundation/AVFoundation.h>

     

     

    static NSMutableDictionary *_allID;

    static SystemSoundID _currentData;

     

    @implementation RYAVTool

     

    +(void)load

    {

        if (_allID==nil) {

            _allID =[[NSMutableDictionary alloc]init];

        }

    }

     

    +(NSMutableDictionary*)playAVWithName:(NSString*)name

    {

        if (!name) {

            return _allID;

        }

        

        NSString*path=[[NSBundle mainBundle]pathForResource:name ofType:nil];

        if (!path) {

            return _allID;

        }

        NSURL *url=[NSURL fileURLWithPath:path];

        _currentData=[_allID[name] unsignedIntValue];

        if (!_currentData) {

            //创建音频播放文件

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

            //保存

            _allID[name]=@(_currentData);

        }

        //播放

        AudioServicesPlayAlertSound(_currentData);

        

        return _allID;

    }

    @end

  • 相关阅读:
    vue-搜索功能-实时监听搜索框的输入,N毫秒请求一次数据
    vue-注册全局过滤器
    vue-nuxt--切换布局文件
    vue.js 分页加载,向上滑动,依次加载数据。
    Vue.js项目引入less文件报错解决
    小程序/js监听输入框验证金额
    React 安装
    类垂直站点插件实现与分享
    多维度论怎样在日常中提升
    node.js的安装环境搭建
  • 原文地址:https://www.cnblogs.com/tangranyang/p/4662504.html
Copyright © 2011-2022 走看看