avaudioPlayer无声音四大原因
1,是否将AudioPlayer声明为全局变量;
2,本地音乐文件是否存在,路径是否正确;
3,初始化NSURL时,是否使用的是 fileURLWithPath . (如果错用 URLWithString 也是会造成初始化失败的)
4.播放前是否加入 [[AVAudioSession sharedInstance]setCategory:AVAudioSessionCategoryPlayback error:nil]; //将之前的声音全部停止
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
NSString *soundPath=[[NSBundle mainBundle] pathForResource:name ofType:@"mp3"];
NSURL *soundUrl=[[NSURL alloc] initFileURLWithPath:soundPath];
NSError *err = nil;
self.movePlayer =[[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:&err];
[self.movePlayer prepareToPlay];
if (err != nil) {
NSLog(@"move player init error:%@",err);
}else{
[self.movePlayer play];
}