我正在使用 MPMoviePlayerController 播放循环播放的视频剪辑。在应用委托(delegate)中,我将 AVAudioSession 的类别设置为 AVAudioSessionCategoryAmbient,这样我的视频剪辑就不会中断 iPod 音频。
这在设备上播放 iPod 音频时效果很好,但在使用 AirPlay 时,每次我的循环视频重新开始时音频都会短暂中断,这种情况非常频繁(而且很烦人)。
在 AppDelegate.m 中:
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:@"AVAudioSessionCategoryAmbient" error:nil];
在我的视频 View Controller 中:
self.videoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
self.videoPlayer.useApplicationAudioSession = YES;
self.videoPlayer.controlStyle = MPMovieControlStyleNone;
self.videoPlayer.repeatMode = MPMovieRepeatModeOne;
我已经在网上搜索过了,似乎找不到答案。任何建议都会很棒。谢谢!
最佳答案
问题解决了!我只需要使用 AVPlayer 而不是 MPMoviePlayerController。
self.avPlayer = [AVPlayer playerWithURL:url];
self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
self.avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
self.avPlayerLayer.frame = self.view.bounds;
[self.view.layer addSublayer:self.avPlayerLayer];
[self.avPlayer play];
并使其循环:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(movieDidFinish:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[self.avPlayer currentItem]];
...
- (void)movieDidFinish:(NSNotification *)notification {
[self.avPlayer seekToTime:kCMTimeZero];
}
关于ios - MPMoviePlayerController 中断 AirPlay iPod 音频,但它在设备上运行良好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11767693/