MPMoviePlayerController会中断AirPlay iPod音频,但它可以在设备上正常工作

时间:2012-08-01 21:15:03

标签: ios audio video mpmovieplayercontroller

我正在使用MPMoviePlayerController播放循环播放的视频片段。在app delegate中,我将AVAudioSession的类别设置为 AVAudioSessionCategoryAmbient ,以便我的视频剪辑不会中断iPod音频。

在设备上播放iPod音频时效果很好,但使用AirPlay时,每次循环播放视频时音频都会短暂中断,这很常见(而且很烦人)。

在AppDelegate.m中:

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:@"AVAudioSessionCategoryAmbient" error:nil];

在我的视频观看控制器中:

self.videoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
self.videoPlayer.useApplicationAudioSession = YES;
self.videoPlayer.controlStyle = MPMovieControlStyleNone;
self.videoPlayer.repeatMode = MPMovieRepeatModeOne;
我已经在网上搜索,似乎无法找到答案。任何建议都会很棒。谢谢!

1 个答案:

答案 0 :(得分:2)

问题解决了!我只需要使用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];
}