iOS開發拓展篇—音樂的播放
一、簡單說明
音樂播放用到一個叫做AVAudioPlayer的類,這個類可以用於播放手機本地的音樂文件。
注意:
(1)該類(AVAudioPlayer)只能用於播放本地音頻。
(2)時間比較短的(稱之為音效)使用AudioServicesCreateSystemSoundID來創建,而本地時間較長(稱之為音樂)使用AVAudioPlayer類。
二、代碼示例
AVAudioPlayer類依賴於AVFoundation框架,因此使用該類必須先導入AVFoundation框架,並包含其頭文件(包含主頭文件即可)。
導入必要的,需要播放的音頻文件到項目中。
代碼示例:
1 // 2 // YYViewController.m 3 // 15-播放音樂 4 // 5 6 #import "YYViewController.h" 7 #import <AVFoundation/AVFoundation.h> 8 9 @interface YYViewController () 10 11 @end 12 13 @implementation YYViewController 14 15 - (void)viewDidLoad 16 { 17 [super viewDidLoad]; 18 19 } 20 21 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 22 { 23 24 //1.音頻文件的url路徑 25 NSURL *url=[[NSBundle mainBundle]URLForResource:@"235319.mp3" withExtension:Nil]; 26 27 //2.創建播放器(注意:一個AVAudioPlayer只能播放一個url) 28 AVAudioPlayer *audioPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil]; 29 30 //3.緩沖 31 [audioPlayer prepareToPlay]; 32 33 //4.播放 34 [audioPlayer play]; 35 } 36 37 @end
代碼說明:運行程序,點擊模擬器界面,卻並沒有能夠播放音頻文件,原因是代碼中創建的AVAudioPlayer播放器是一個局部變量,應該調整為全局屬性。
可將代碼調整如下,即可播放音頻:
1 #import "YYViewController.h" 2 #import <AVFoundation/AVFoundation.h> 3 4 @interface YYViewController () 5 @property(nonatomic,strong)AVAudioPlayer *audioplayer; 6 @end 7 8 @implementation YYViewController 9 10 - (void)viewDidLoad 11 { 12 [super viewDidLoad]; 13 14 } 15 16 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 17 { 18 19 //1.音頻文件的url路徑 20 NSURL *url=[[NSBundle mainBundle]URLForResource:@"235319.mp3" withExtension:Nil]; 21 22 //2.創建播放器(注意:一個AVAudioPlayer只能播放一個url) 23 self.audioplayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil]; 24 25 //3.緩沖 26 [self.audioplayer prepareToPlay]; 27 28 //4.播放 29 [self.audioplayer play]; 30 } 31 32 @end
注意:一個AVAudioPlayer只能播放一個url,如果想要播放多個文件,那么就得創建多個播放器。
三、相關說明
新建一個項目,在storyboard中放三個按鈕,分別用來控制音樂的播放、暫停和停止。
程序代碼如下:
1 #import "YYViewController.h" 2 #import <AVFoundation/AVFoundation.h> 3 4 @interface YYViewController () 5 @property(nonatomic,strong)AVAudioPlayer *player; 6 - (IBAction)play; 7 - (IBAction)pause; 8 - (IBAction)stop; 9 @end 10 11 @implementation YYViewController 12 13 - (void)viewDidLoad 14 { 15 [super viewDidLoad]; 16 17 //1.音頻文件的url路徑 18 NSURL *url=[[NSBundle mainBundle]URLForResource:@"235319.mp3" withExtension:Nil]; 19 20 //2.創建播放器(注意:一個AVAudioPlayer只能播放一個url) 21 self.player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil]; 22 23 //3.緩沖 24 [self.player prepareToPlay]; 25 26 } 27 28 - (IBAction)play { 29 //開始播放/繼續播放 30 [self.player play]; 31 } 32 33 - (IBAction)pause { 34 //暫停 35 [self.player pause]; 36 } 37 38 - (IBAction)stop { 39 //停止 40 //注意:如果點擊了stop,那么一定要讓播放器重新創建,否則會出現一些莫名其面的問題 41 [self.player stop]; 42 } 43 @end
注意:如果點了“停止”,那么一定要播放器重新創建,不然的話會出現莫名其妙的問題。
點擊了stop之后,播放器實際上就不能再繼續使用了,如果還繼續使用,那么后續的一些東西會無法控制。
推薦代碼:
1 #import "YYViewController.h" 2 #import <AVFoundation/AVFoundation.h> 3 4 @interface YYViewController () 5 @property(nonatomic,strong)AVAudioPlayer *player; 6 - (IBAction)play; 7 - (IBAction)pause; 8 - (IBAction)stop; 9 @end 10 11 @implementation YYViewController 12 13 #pragma mark-懶加載 14 -(AVAudioPlayer *)player 15 { 16 if (_player==Nil) { 17 18 //1.音頻文件的url路徑 19 NSURL *url=[[NSBundle mainBundle]URLForResource:@"235319.mp3" withExtension:Nil]; 20 21 //2.創建播放器(注意:一個AVAudioPlayer只能播放一個url) 22 self.player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil]; 23 24 //3.緩沖 25 [self.player prepareToPlay]; 26 } 27 return _player; 28 } 29 30 - (void)viewDidLoad 31 { 32 [super viewDidLoad]; 33 } 34 35 - (IBAction)play { 36 //開始播放/繼續播放 37 [self.player play]; 38 } 39 40 - (IBAction)pause { 41 //暫停 42 [self.player pause]; 43 } 44 45 - (IBAction)stop { 46 //停止 47 //注意:如果點擊了stop,那么一定要讓播放器重新創建,否則會出現一些莫名其面的問題 48 [self.player stop]; 49 self.player=Nil; 50 } 51 @end
如果點擊了停止按鈕,那么音樂會從頭開始播放。
四、播放多個文件
點擊,url,按住common建查看。
可以發現,這個url是只讀的,因此只能通過initWithContentsOfUrl的方式進行設置,也就意味着一個播放器對象只能播放一個音頻文件。
那么如何實現播放多個音頻文件呢?
可以考慮封裝一個播放音樂的工具類,下一篇文章將會介紹具體怎么實現。