AVPlayer 空白视频:播放声音但没有视频
AVPlayer blank video: playing sound but no video
This question遇到了同样的问题,但是解决方案都没有用。
AVPlayer
有时播放空白视频:有声音没有视频。
在播放空白视频的同时,我们打印了播放器的帧和状态。框架不为零且设置正确。状态也是readyToPlay
。 play
函数也在主线程上被调用。
也就是说,播放器层的框架是有效的,播放器也可以播放了。然而没有视频出现,尽管有声音。
这个问题似乎是一个时机问题。如果我们在播放视频之前等待 5-10 秒,视频每次都能正常播放。
此问题出现在 iOS 10,而不是 iOS 8 或 9。
Apple 论坛上的 This thread 表明它可能是与以下内容相关的错误
AVVideoCompositionCoreAnimationTool
,我们也用。
有什么解决办法吗?
这种情况在 iPhone 7 台设备上比在 iPhone 5 台设备上更常见。
代码:
fileprivate func playVideo(_ videoURL: String, prompt: String) {
// Show <playerView>
playerView.isHidden = false
// Use new video in player
playerItem = AVPlayerItem(url: URL(fileURLWithPath: videoURL))
print("STATUS: \(player.status == AVPlayerStatus.readyToPlay). FRAME: \(playerLayer.frame). MAIN THREAD: \(Thread.isMainThread)")
player.replaceCurrentItem(with: playerItem)
// Start playing video
player.seek(to: kCMTimeZero)
player.actionAtItemEnd = .none
player.play()
}
我认为您需要使用 AVPlayerViewController 或 AVPlayerLayer。
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
//set player layer frame and attach it to our view
playerLayer.frame = self.containerView.bounds;
[self.containerView.layer addSublayer:playerLayer];
//play the video
[player play];
来自Apple doc:
AVPlayer and AVPlayerItem are nonvisual objects meaning that on their
own are unable to present an asset’s video on screen. You have two
primary approaches you can use to present your video content on
screen:
AVKit: The best way to present your video content is by using the
AVKit framework’s AVPlayerViewController class in iOS and tvOS or the
AVPlayerView class in macOS. These classes present the video content,
along with playback controls and other media features giving you a
full-featured playback experience.
AVPlayerLayer: If you are building a custom interface for your player,
you use a Core Animation CALayer subclass provided by AVFoundation
called AVPlayerLayer. The player layer can be set as a view’s backing
layer or can be added directly to the layer hierarchy. Unlike
AVPlayerView and AVPlayerViewController, a player layer doesn’t
present any playback controls, but simply presents the visual content
on screen. It is up to you to build the playback transport controls to
play, pause, and seek through the media.
万一有人遇到这个问题,这个问题似乎与 iOS 错误有关。该问题不会出现在 iOS 8/9 设备上,只会出现在新的 iOS 10 设备上。从 iOS 10.2 开始,问题消失了。
不幸的是,仍然没有 10.0.x 或 10.1.x.
的编程解决方案
This question遇到了同样的问题,但是解决方案都没有用。
AVPlayer
有时播放空白视频:有声音没有视频。
在播放空白视频的同时,我们打印了播放器的帧和状态。框架不为零且设置正确。状态也是readyToPlay
。 play
函数也在主线程上被调用。
也就是说,播放器层的框架是有效的,播放器也可以播放了。然而没有视频出现,尽管有声音。
这个问题似乎是一个时机问题。如果我们在播放视频之前等待 5-10 秒,视频每次都能正常播放。
此问题出现在 iOS 10,而不是 iOS 8 或 9。
Apple 论坛上的This thread 表明它可能是与以下内容相关的错误
AVVideoCompositionCoreAnimationTool
,我们也用。
有什么解决办法吗?
这种情况在 iPhone 7 台设备上比在 iPhone 5 台设备上更常见。
代码:
fileprivate func playVideo(_ videoURL: String, prompt: String) {
// Show <playerView>
playerView.isHidden = false
// Use new video in player
playerItem = AVPlayerItem(url: URL(fileURLWithPath: videoURL))
print("STATUS: \(player.status == AVPlayerStatus.readyToPlay). FRAME: \(playerLayer.frame). MAIN THREAD: \(Thread.isMainThread)")
player.replaceCurrentItem(with: playerItem)
// Start playing video
player.seek(to: kCMTimeZero)
player.actionAtItemEnd = .none
player.play()
}
我认为您需要使用 AVPlayerViewController 或 AVPlayerLayer。
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
//set player layer frame and attach it to our view
playerLayer.frame = self.containerView.bounds;
[self.containerView.layer addSublayer:playerLayer];
//play the video
[player play];
来自Apple doc:
AVPlayer and AVPlayerItem are nonvisual objects meaning that on their own are unable to present an asset’s video on screen. You have two primary approaches you can use to present your video content on screen:
AVKit: The best way to present your video content is by using the AVKit framework’s AVPlayerViewController class in iOS and tvOS or the AVPlayerView class in macOS. These classes present the video content, along with playback controls and other media features giving you a full-featured playback experience.
AVPlayerLayer: If you are building a custom interface for your player, you use a Core Animation CALayer subclass provided by AVFoundation called AVPlayerLayer. The player layer can be set as a view’s backing layer or can be added directly to the layer hierarchy. Unlike AVPlayerView and AVPlayerViewController, a player layer doesn’t present any playback controls, but simply presents the visual content on screen. It is up to you to build the playback transport controls to play, pause, and seek through the media.
万一有人遇到这个问题,这个问题似乎与 iOS 错误有关。该问题不会出现在 iOS 8/9 设备上,只会出现在新的 iOS 10 设备上。从 iOS 10.2 开始,问题消失了。
不幸的是,仍然没有 10.0.x 或 10.1.x.
的编程解决方案