码迷,mamicode.com
首页 > 移动开发 > 详细

ios程序播放音频文件

时间:2015-07-20 21:29:55      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

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

ios程序播放音频文件

标签:

原文地址:http://www.cnblogs.com/tangranyang/p/4662504.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!