AVPlayer blank video: playing sound but no video

北城余情 提交于 2019-12-07 22:57:11

问题


This question has the same problem, but the solutions didn't work.

The AVPlayer sometimes plays a blank video: there is sound but no video.

While blank videos were played, we printed the frame and status of the player. The frame was non-zero and was correctly set. The status was also readyToPlay. The play function is also invoked on the main thread.

In other words, the frame for the player layer is valid, and the player is also ready to play. Yet no video appears, even though sound does.

The issue seems to be a timing one. If we wait 5-10 seconds before playing the video, the video works fine each time.

This issue appears on iOS 10, not iOS 8 or 9.

This thread on the Apple forums suggests it might be a bug related to AVVideoCompositionCoreAnimationTool, which we also use.

Any solutions?

This happens more often on iPhone 7 devices than iPhone 5s devices.

Code:

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()
}

回答1:


In case anyone encounters this issue, the problem seems related to an iOS bug. The problem doesn't occur on iOS 8/9 devices, only on new iOS 10 devices. Starting with iOS 10.2, the problem vanishes.

Unfortunately, still no programmatic solution for 10.0.x or 10.1.x.




回答2:


I think you need to play with a AVPlayerViewController or 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];

From 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.



来源:https://stackoverflow.com/questions/41052277/avplayer-blank-video-playing-sound-but-no-video

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!