使用AVAudioPlayer播放本地音乐文件。
1,声明全局AudioPlayer变量:
[objc] view plain copy print?在CODE上查看代码片派生到我的代码片
@property(nonatomic,strong)AVAudioPlayer *movePlayer ;
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];
注意:如果播放失败,请注意检查: