MPMoviePlayerController打断了AirPlay iPod音频,但在设备上正常工作

3
我正在使用MPMoviePlayerController播放一个循环的视频片段。在应用程序代理中,我将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;

我已经搜索了整个网络,但似乎找不到答案。任何建议都将不胜感激。谢谢!

(这段话与IT技术无关,仅为询问帮助的一般用语)
1个回答

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];
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接