播放音频和视频无声音的原因

播放本地音频

//注意事项:AVAudioPlayer对象必须是全局的,否则无法播放
//也可用路径的方式:_audioPlay = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];

NSData *fileData = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMapped error:nil];
NSError *playerError;
_audioPlay = [[AVAudioPlayer alloc]initWithData:fileData error:&playerError];
_audioPlay.volume = 1;
[_audioPlay prepareToPlay];
[_audioPlay play];

播放本地视频

 NSURL *videoURL= [NSURL fileURLWithPath:videoPath];
_moviePlayer = [[MPMoviePlayerViewController  alloc]initWithContentURL:videoURL];
[self presentViewController:_moviePlayer animated:YES completion:nil];

结果两者都没有声音,什么原因呢?

必须要事先添加下面的代码!!!

AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
[session setActive:YES error:nil];

你可能感兴趣的:(播放音频和视频无声音的原因)