1.播放網(wǎng)絡(luò)音頻
NSURL * url? =[NSURL URLWithString:MP3URL];
AVPlayerItem* songItem =[[AVPlayerItem alloc]initWithURL:url];
self.avplayer=[[AVPlayer alloc]initWithPlayerItem:songItem];
[self.avplayer play];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished) name:AVPlayerItemDidPlayToEndTimeNotificationobject:songItem];
2.播放本地音頻
NSString *tmp=[[NSBundle mainBundle].resourcePath stringByAppendingPathComponent:@"audio/move.mp3"];
NSLog(@"%@",tmp);
NSURL*moveMP3=[NSURL fileURLWithPath:[[NSBundle mainBundle].resourcePath stringByAppendingPathComponent:@"audio/move.mp3"]];
NSError*err=nil;
self.movePlayer=[[AVAudioPlayer alloc] initWithContentsOfURL:moveMP3 error:&err];
self.movePlayer.volume=1.0;
[self.movePlayer prepareToPlay];if(err!=nil) {
NSLog(@"move player init error:%@",err);
}else{
[self.movePlayer play];
注意:
1.兩種方法player必須是全局的,否則播放沒有聲音。
2.本地音樂文件是否存在负蠕,路徑是否正確哺徊;
3.初始化NSURL時,是否使用的是 ?fileURLWithPath . (如果錯用URLWithString 也是會造成初始化失敗的)
參考: