我正在使用 AVPlayerViewController
在 iPad 应用程序上播放视频文件(H.264、AAC、MP4 容器)。
在 iOS 10 中一切正常。在 iOS 11 中它也能正确播放视频。
但在 iOS 11 中,当我开始向任何方向滑动时,它会立即关闭视频并静音。它还在底部的时间线旁边显示了一个加载指示器。
它还忽略了 allowsPictureInPicturePlayback
属性,因此它不会在 iOS 11 上显示画中画按钮。
这是我使用的代码:
avPlayerController = AVPlayerViewController()
avPlayerController?.showsPlaybackControls = true
avPlayerController?.allowsPictureInPicturePlayback = true
avPlayerController?.player = AVPlayer(url: videoUrl as URL)
avPlayerController?.player?.play()
self.present(self.avPlayerController!, animated: true, completion: nil)
avPlayerController?.player?.actionAtItemEnd = AVPlayerActionAtItemEnd.none
NotificationCenter.default.addObserver(self, selector: #selector(onVideoCompleted), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: self.avPlayerController!.player?.currentItem)
以及在视频结束时关闭视频播放器的函数:
func onVideoCompleted(notification:Notification) {
self.avPlayerController?.player = nil
self.avPlayerController?.dismiss(animated: true, completion: nil)
}
当屏幕变黑时,我在控制台中得到了这个:
AVOutputDeviceDiscoverySession (FigRouteDiscoverer)
>>>> -[AVFigRouteDiscovererOutputDeviceDiscoverySessionImpl
outputDeviceDiscoverySessionDidChangeDiscoveryMode:]: Setting device discovery
mode to DiscoveryMode_Presence (client: MyAppName)
最佳答案
好吧,我发现了错误: 要在按下“完成”时关闭 Airplay 视频播放,我使用了以下代码:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if avPlayerController?.isBeingDismissed ?? false {
avPlayerController?.player = nil
}
}
但在 iOS 11 中,Apple 添加了一项功能,可通过滑动手势关闭视频播放器。因此,当我滑动时,将调用 viewWillAppear
函数。
将这段代码放在 viewDidAppear
中修复了这个问题并保留了 AirPlay-Fix。
关于swift - 在 iOS 11 上滑动时 AVPlayerViewController 黑屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46321860/