我正在尝试使用 AirPlay 让 AVPlayer 在 AppleTV 上播放。这看起来很简单,根据苹果文档,它应该受到支持。
代码如下:
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithAsset:_composition];
playerItem.audioMix = _audioMix;
playerItem.videoComposition = _videoComposition;
_currentPlayer = [[AVPlayer alloc] initWithPlayerItem:playerItem];
[_currentPlayer setAllowsExternalPlayback:YES];
[_currentPlayer setUsesExternalPlaybackWhileExternalScreenIsActive:YES];
[_currentPlayer setAllowsAirPlayVideo:YES];
[_currentPlayer setUsesAirPlayVideoWhileAirPlayScreenIsActive:YES];
[_currentPlayer addObserver:self forKeyPath:@"rate" options:0 context:nil];
[_currentPlayer addObserver:self forKeyPath:@"externalPlaybackActive" options:0 context:nil];
我注意到的一件事是,当我打开 AirPlay 时,播放时间似乎并没有向前移动,有时我认为它实际上会向后跳一两秒。
有什么想法吗?
旁注:
- 该合成是使用 AVFoundation 构建的,视频格式为 H.264。
- 如果我打开镜像并将 View 推送到第二个屏幕,它就会起作用
- 如果没有视频播放,声音也会起作用。
最佳答案
我也遇到了同样的问题,我疯狂了好几个小时,直到找到答案!
将其设置为NO
[_currentPlayer setAllowsExternalPlayback:NO];
并删除此行
[_currentPlayer setUsesExternalPlaybackWhileExternalScreenIsActive:YES];
它会起作用的!
关于avfoundation - 如何让AVPlayer在不镜像的情况下与AirPlay一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18176703/