ios - MPMoviePlayerController 中断 AirPlay iPod 音频,但它在设备上运行良好

标签 ios audio video mpmovieplayercontroller

我正在使用 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/

相关文章:

objective-c - 设置事件指示器直到从服务器加载图像?

html - Windows上的Safari无法播放音频

HTML <video loop> - 下载多次

video - 如何连接具有不同属性的ffmpeg中的视频?

video - libdash mpd 解析器与自定义播放器的集成

ios - 找不到 Facebook.h

iphone - 在 RSA 解密中将 uint8_t 转换为 NSString

ios 应用程序在模拟器 5.0 中崩溃

c# - 使用重叠窗口计算 RMS

vb.net - 如何录制特定应用程序的音频? VB.Net