gpt4 book ai didi

ios - AVPlayer 空白视频 : playing sound but no video

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:02:30 25 4
gpt4 key购买 nike

This question有同样的问题,但解决方案无效。

AVPlayer 有时会播放空白视频:有声音但没有视频。

在播放空白视频时,我们打印了播放器的帧和状态。框架不为零且设置正确。状态也是 readyToPlayplay 函数也在主线程上被调用。

换句话说,播放器层的框架是有效的,播放器也可以播放了。然而没有视频出现,尽管有声音。

这个问题似乎是一个时机问题。如果我们在播放视频之前等待 5-10 秒,视频每次都能正常播放。

此问题出现在 iOS 10 上,而不是 iOS 8 或 9。

This thread在 Apple 论坛上表明它可能是与以下内容相关的错误AVVideoCompositionCoreAnimationTool,我们也使用它。

有什么解决办法吗?

与 iPhone 5s 设备相比,这种情况在 iPhone 7 设备上更常见。

代码:

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 - AVPlayer 空白视频 : playing sound but no video,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41052277/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com