Play audio Files (播放音频文件)
2014-01-21 15:49
351 查看
1。必须导入
#import <AVFoundation/AVFoundation.h>
2。实现协议: <AVAudioPlayerDelegate>
e.g.
@property (nonatomic, strong)AVAudioPlayer *audioPlayer;
- (void)actionPlay {
//为后续异步加载,使用并行全局队列
dispatch_queue_t dispatchQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//加载本地音频资源 bundle -> file -> data ,因为有可能音频文件比较大,所以采用异步加载
dispatch_async(dispatchQueue, ^(void) {
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *filePath = [mainBundle pathForResource:@"MySong" ofType:@"mp3"];
NSData *fileData = [NSData dataWithContentsOfFile:filePath];
NSError *error = nil;
/* 当模拟器有声音,真机无声音时 需在启动播放器之前 加上以下这一句 */
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
/* 初始化 音频播放器 */
self.audioPlayer = [[AVAudioPlayer alloc] initWithData:fileData error:&error];
if (self.audioPlayer != nil){
/* 设置代理,启动 */
self.audioPlayer.delegate = self;
if ([self.audioPlayer prepareToPlay] && [self.audioPlayer play]){
NSLog(@"Successfully started playing");
} else {
NSLog(@"Failed to play");
}
} else {
NSLog(@"Failed to instantiate AVAudioPlayer");
}
});
}
//可在结束播放时,释放(无论是正常停止,或是意外停止) 参数flag : 播放是否成功结束
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
NSLog(@"Finished playing the song");
if ([player isEqual:self.audioPlayer]){
self.audioPlayer = nil;
} else {
/* 其它播放器不作处理 */
}
}
//被中断,如来电等
- (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player{
/* Audio Session is interrupted. The player will be paused here */
}
- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player withOptions:(NSUInteger)flags{
if (flags == AVAudioSessionInterruptionOptionShouldResume && player != nil){
[player play];
}
}
#import <AVFoundation/AVFoundation.h>
2。实现协议: <AVAudioPlayerDelegate>
e.g.
@property (nonatomic, strong)AVAudioPlayer *audioPlayer;
- (void)actionPlay {
//为后续异步加载,使用并行全局队列
dispatch_queue_t dispatchQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//加载本地音频资源 bundle -> file -> data ,因为有可能音频文件比较大,所以采用异步加载
dispatch_async(dispatchQueue, ^(void) {
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *filePath = [mainBundle pathForResource:@"MySong" ofType:@"mp3"];
NSData *fileData = [NSData dataWithContentsOfFile:filePath];
NSError *error = nil;
/* 当模拟器有声音,真机无声音时 需在启动播放器之前 加上以下这一句 */
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
/* 初始化 音频播放器 */
self.audioPlayer = [[AVAudioPlayer alloc] initWithData:fileData error:&error];
if (self.audioPlayer != nil){
/* 设置代理,启动 */
self.audioPlayer.delegate = self;
if ([self.audioPlayer prepareToPlay] && [self.audioPlayer play]){
NSLog(@"Successfully started playing");
} else {
NSLog(@"Failed to play");
}
} else {
NSLog(@"Failed to instantiate AVAudioPlayer");
}
});
}
//可在结束播放时,释放(无论是正常停止,或是意外停止) 参数flag : 播放是否成功结束
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
NSLog(@"Finished playing the song");
if ([player isEqual:self.audioPlayer]){
self.audioPlayer = nil;
} else {
/* 其它播放器不作处理 */
}
}
//被中断,如来电等
- (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player{
/* Audio Session is interrupted. The player will be paused here */
}
- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player withOptions:(NSUInteger)flags{
if (flags == AVAudioSessionInterruptionOptionShouldResume && player != nil){
[player play];
}
}
相关文章推荐
- IOS音频1:之采用四种方式播放音频文件(一)AudioToolbox AVFoundation OpenAL AUDIO QUEUE
- iphone利用AudioQueue播放音频文件(mp3,aac,caf,wav等)
- 使用audio标签播放音频文件
- IOS音频1:之采用四种方式播放音频文件(一)AudioToolbox AVFoundation OpenAL AUDIO QUEUE
- audio标签播放音频二进制文件
- 微信浏览器中audio 自动播放音频文件
- Android音频处理之通过AudioRecord去保存PCM文件进行录制,播放,停止,删除功能
- 利用AudioQueue播放音频文件(mp3,aac,caf,wav等)
- IOS音频2:之采用四种方式播放音频文件(二)AudioToolbox AVFoundation OpenAL AUDIO QUEUE(2014-10-17 16:07)
- iphone利用AudioQueue播放音频文件(mp3,aac,caf,wav等)
- 用 Qt 的 QAudioOutput 类播放 WAV 音频文件(使用了libsndfile外部库)
- CEF3 HTML5 audio标签为什么不能播放mp3格式的音频文件
- IOS音频1:之采用四种方式播放音频文件(一)AudioToolbox AVFoundation OpenAL AUDIO QUEUE
- symbian 如何播放声音文件Playing audio files
- HTML5 API---使用WebAudio API播放音频文件
- H5页面实现一个Audio标签加载多个音频文件,并进行播放和展示音频长度
- h5 audio播放音频文件
- 利用AVAudioPlay播放本地Bundle下一个音频文件.mp3
- IOS音频3:之采用四种方式播放音频文件(三)AudioToolbox AVFoundation OpenAL AUDIO QUEUE