iOS開發拓展篇—封裝音頻文件播放工具類數組
1、簡單說明app
1.關於音樂播放的簡單說明異步
(1)音樂播放用到一個叫作AVAudioPlayer的類函數
(2)AVAudioPlayer經常使用方法工具
加載音樂文件oop
- (id)initWithContentsOfURL:(NSURL *)url error:(NSError **)outError;測試
- (id)initWithData:(NSData *)data error:(NSError **)outError;atom
準備播放(緩衝,提升播放的流暢性) - (BOOL)prepareToPlay;url
播放(異步播放)- (BOOL)play;spa
暫停 - (void)pause;
中止- (void)stop;
是否正在播放 @property(readonly, getter=isPlaying) BOOL playing;
時長 @property(readonly) NSTimeInterval duration;
當前的播放位 @property NSTimeInterval currentTime;
播放次數(-1表明無限循環播放,其餘表明播放numberOfLoops+1次 @property NSInteger numberOfLoops;
音量 @property float volume;
是否容許更改速率@property BOOL enableRate;
播放速率(1是正常速率,0.5是通常速率,2是雙倍速率) @property float rate;
有多少個聲道 @property(readonly) NSUInteger numberOfChannels;
2.播放多個音樂文件
說明:若是要播放多個音樂文件,那麼最傻瓜的作法是,建立多個全局的播放器去播放對應的音樂文件,可是這種方法沒法適用於須要播放的文件數量巨大的狀況。
1 // 2 // YYAudioTool.h 3 // 17-多個音樂文件的播放 4 // 5 // Created by apple on 14-8-9. 6 // Copyright (c) 2014年 yangyong. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h> 10 #import <AVFoundation/AVFoundation.h> 11 @interface YYAudioTool : NSObject 12 /** 13 *播放音樂文件 14 */ 15 +(BOOL)playMusic:(NSString *)filename; 16 /** 17 *暫停播放 18 */ 19 +(void)pauseMusic:(NSString *)filename; 20 /** 21 *播放音樂文件 22 */ 23 +(void)stopMusic:(NSString *)filename; 24 @end
YYAudioTool.m文件
1 // 2 // YYAudioTool.m 3 // 17-多個音樂文件的播放 4 // 5 // Created by apple on 14-8-9. 6 // Copyright (c) 2014年 yangyong. All rights reserved. 7 // 8 9 #import "YYAudioTool.h" 10 11 @implementation YYAudioTool 12 /** 13 *存放全部的音樂播放器 14 */ 15 static NSMutableDictionary *_musices; 16 +(NSMutableDictionary *)musices 17 { 18 if (_musices==nil) { 19 _musices=[NSMutableDictionary dictionary]; 20 } 21 return _musices; 22 } 23 24 /** 25 *播放音樂 26 */ 27 +(BOOL)playMusic:(NSString *)filename 28 { 29 if (!filename) return NO;//若是沒有傳入文件名,那麼直接返回 30 //1.取出對應的播放器 31 AVAudioPlayer *player=[self musices][filename]; 32 33 //2.若是播放器沒有建立,那麼就進行初始化 34 if (!player) { 35 //2.1音頻文件的URL 36 NSURL *url=[[NSBundle mainBundle]URLForResource:filename withExtension:nil]; 37 if (!url) return NO;//若是url爲空,那麼直接返回 38 39 //2.2建立播放器 40 player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil]; 41 42 //2.3緩衝 43 if (![player prepareToPlay]) return NO;//若是緩衝失敗,那麼就直接返回 44 45 //2.4存入字典 46 [self musices][filename]=player; 47 } 48 49 //3.播放 50 if (![player isPlaying]) { 51 //若是當前沒處於播放狀態,那麼就播放 52 return [player play]; 53 } 54 55 return YES;//正在播放,那麼就返回YES 56 } 57 58 +(void)pauseMusic:(NSString *)filename 59 { 60 if (!filename) return;//若是沒有傳入文件名,那麼就直接返回 61 62 //1.取出對應的播放器 63 AVAudioPlayer *player=[self musices][filename]; 64 65 //2.暫停 66 [player pause];//若是palyer爲空,那至關於[nil pause],所以這裏能夠不用作處理 67 68 } 69 70 +(void)stopMusic:(NSString *)filename 71 { 72 if (!filename) return;//若是沒有傳入文件名,那麼就直接返回 73 74 //1.取出對應的播放器 75 AVAudioPlayer *player=[self musices][filename]; 76 77 //2.中止 78 [player stop]; 79 80 //3.將播放器從字典中移除 81 [[self musices] removeObjectForKey:filename]; 82 } 83 @end
測試程序:
在storyboard中拖拽控件,並進行連線,以作控制。
導入可供播放的音樂素材。
測試程序的代碼設計以下:
1 // 2 // YYViewController.m 3 // 17-多個音樂文件的播放 4 // 5 // Created by apple on 14-8-9. 6 // Copyright (c) 2014年 yangyong. All rights reserved. 7 // 8 9 #import "YYViewController.h" 10 #import "YYAudioTool.h" 11 12 @interface YYViewController () 13 - (IBAction)play; 14 - (IBAction)pause; 15 - (IBAction)stop; 16 - (IBAction)next; 17 18 //用一個數組來保存全部的音樂文件 19 @property(nonatomic,strong)NSArray *songs; 20 //用一個int型的屬性來記錄當前的索引 21 @property(nonatomic,assign)int currentIndex; 22 @end 23 24 @implementation YYViewController 25 #pragma mark-懶加載 26 -(NSArray *)songs 27 { 28 if (_songs==nil) { 29 self.songs=@[@"235319.mp3",@"309769.mp3",@"120125029.mp3"]; 30 } 31 return _songs; 32 } 33 34 - (void)viewDidLoad 35 { 36 [super viewDidLoad]; 37 } 38 39 - (IBAction)play { 40 //開始播放/繼續播放 41 [YYAudioTool playMusic:self.songs[self.currentIndex]]; 42 } 43 44 - (IBAction)pause { 45 //暫停播放 46 [YYAudioTool pauseMusic:self.songs[self.currentIndex]]; 47 } 48 49 - (IBAction)stop { 50 //中止播放 51 [YYAudioTool stopMusic:self.songs[self.currentIndex]]; 52 } 53 54 //播放下一首 55 - (IBAction)next { 56 //1.先中止當前播放 57 [self stop]; 58 59 //2.設置當前索引+1 60 self.currentIndex++; 61 if (self.currentIndex>=self.songs.count) { 62 self.currentIndex=0; 63 } 64 65 //3.播放音樂 66 [self play]; 67 } 68 @end
2、對工具類進行改造,讓其兼能播放音效文件
說明:
音效只有建立、播放和銷燬(中止)三個操做,由於音效通常都很短,所以沒有暫停的方法。
把對音效文件的播放加入到工具類中,實現的代碼以下:
YYAudioTool.h文件
1 // 2 // YYAudioTool.h 3 // 17-多個音樂文件的播放 4 // 5 // Created by apple on 14-8-9. 6 // Copyright (c) 2014年 yangyong. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h> 10 #import <AVFoundation/AVFoundation.h> 11 @interface YYAudioTool : NSObject 12 /** 13 *播放音樂文件 14 */ 15 +(BOOL)playMusic:(NSString *)filename; 16 /** 17 *暫停播放 18 */ 19 +(void)pauseMusic:(NSString *)filename; 20 /** 21 *播放音樂文件 22 */ 23 +(void)stopMusic:(NSString *)filename; 24 25 /** 26 *播放音效文件 27 */ 28 +(void)playSound:(NSString *)filename; 29 /** 30 *銷燬音效 31 */ 32 +(void)disposeSound:(NSString *)filename; 33 @end
YYAudioTool.m文件
1 // 2 // YYAudioTool.m 3 // 17-多個音樂文件的播放 4 // 5 // Created by apple on 14-8-9. 6 // Copyright (c) 2014年 yangyong. All rights reserved. 7 // 8 9 #import "YYAudioTool.h" 10 11 @implementation YYAudioTool 12 /** 13 *存放全部的音樂播放器 14 */ 15 static NSMutableDictionary *_musicPlayers; 16 +(NSMutableDictionary *)musicPlayers 17 { 18 if (_musicPlayers==nil) { 19 _musicPlayers=[NSMutableDictionary dictionary]; 20 } 21 return _musicPlayers; 22 } 23 24 /** 25 *存放全部的音效ID 26 */ 27 static NSMutableDictionary *_soundIDs; 28 +(NSMutableDictionary *)soundIDs 29 { 30 if (_soundIDs==nil) { 31 _soundIDs=[NSMutableDictionary dictionary]; 32 } 33 return _soundIDs; 34 } 35 36 37 /** 38 *播放音樂 39 */ 40 +(BOOL)playMusic:(NSString *)filename 41 { 42 if (!filename) return NO;//若是沒有傳入文件名,那麼直接返回 43 //1.取出對應的播放器 44 AVAudioPlayer *player=[self musicPlayers][filename]; 45 46 //2.若是播放器沒有建立,那麼就進行初始化 47 if (!player) { 48 //2.1音頻文件的URL 49 NSURL *url=[[NSBundle mainBundle]URLForResource:filename withExtension:nil]; 50 if (!url) return NO;//若是url爲空,那麼直接返回 51 52 //2.2建立播放器 53 player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil]; 54 55 //2.3緩衝 56 if (![player prepareToPlay]) return NO;//若是緩衝失敗,那麼就直接返回 57 58 //2.4存入字典 59 [self musicPlayers][filename]=player; 60 } 61 62 //3.播放 63 if (![player isPlaying]) { 64 //若是當前沒處於播放狀態,那麼就播放 65 return [player play]; 66 } 67 68 return YES;//正在播放,那麼就返回YES 69 } 70 71 +(void)pauseMusic:(NSString *)filename 72 { 73 if (!filename) return;//若是沒有傳入文件名,那麼就直接返回 74 75 //1.取出對應的播放器 76 AVAudioPlayer *player=[self musicPlayers][filename]; 77 78 //2.暫停 79 [player pause];//若是palyer爲空,那至關於[nil pause],所以這裏能夠不用作處理 80 81 } 82 83 +(void)stopMusic:(NSString *)filename 84 { 85 if (!filename) return;//若是沒有傳入文件名,那麼就直接返回 86 87 //1.取出對應的播放器 88 AVAudioPlayer *player=[self musicPlayers][filename]; 89 90 //2.中止 91 [player stop]; 92 93 //3.將播放器從字典中移除 94 [[self musicPlayers] removeObjectForKey:filename]; 95 } 96 97 //播放音效 98 +(void)playSound:(NSString *)filename 99 { 100 if (!filename) return; 101 //1.取出對應的音效 102 SystemSoundID soundID=[[self soundIDs][filename] unsignedIntegerValue]; 103 104 //2.播放音效 105 //2.1若是音效ID不存在,那麼就建立 106 if (!soundID) { 107 108 //音效文件的URL 109 NSURL *url=[[NSBundle mainBundle]URLForResource:filename withExtension:nil]; 110 if (!url) return;//若是URL不存在,那麼就直接返回 111 112 OSStatus status = AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundID); 113 NSLog(@"%ld",status); 114 //存入到字典中 115 [self soundIDs][filename]=@(soundID); 116 } 117 118 //2.2有音效ID後,播放音效 119 AudioServicesPlaySystemSound(soundID); 120 } 121 122 //銷燬音效 123 +(void)disposeSound:(NSString *)filename 124 { 125 //若是傳入的文件名爲空,那麼就直接返回 126 if (!filename) return; 127 128 //1.取出對應的音效 129 SystemSoundID soundID=[[self soundIDs][filename] unsignedIntegerValue]; 130 131 //2.銷燬 132 if (soundID) { 133 AudioServicesDisposeSystemSoundID(soundID); 134 135 //2.1銷燬後,從字典中移除 136 [[self soundIDs]removeObjectForKey:filename]; 137 } 138 } 139 @end
代碼測試:
代碼說明:
打印的值爲0,播放成功(由於函數是C++中的)