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 @end12 13 @implementation YYViewController14 15 - (void)viewDidLoad16 {17 [super viewDidLoad];18 19 }20 21 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event22 {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)viewDidLoad11 {12 [super viewDidLoad];13 14 }15 16 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event17 {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 @end10 11 @implementation YYViewController12 13 - (void)viewDidLoad14 {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 @end10 11 @implementation YYViewController12 13 #pragma mark-懶載入14 -(AVAudioPlayer *)player15 {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)viewDidLoad31 {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複製代碼如果點擊了停止按鈕,那麼音樂會從頭開始播放。