5

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()
}
Community
  • 1
  • 1
Crashalot
  • 33,605
  • 61
  • 269
  • 439
  • can you show your code here? and also the player size displayed on screen – Horst Dec 09 '16 at 03:53
  • @Horst sure updated question. thanks! – Crashalot Dec 09 '16 at 04:01
  • Can you put a project up on github that reproduces the problem? – Rhythmic Fistman Dec 09 '16 at 21:28
  • @RhythmicFistman will try but the project is complex so will take awhile to simplify it so it's easy to debug. also saw your responses on related questions for avplayer issues on ios 10. it could be an ios 10 bug since the code worked on ios 9 and 8 but no longer have those devices to test. – Crashalot Dec 09 '16 at 21:54
  • I have what I believe is a similar issue. Any work around other than waiting 5-10 secs? – crgt Jan 27 '17 at 21:27

2 Answers2

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.

Horst
  • 1,733
  • 15
  • 20
  • right, sorry, omitted the code where we set the frame for the playerlayer. the frame is set properly elsewhere in code, which is verified by the frame at the time the video plays. the issue seems to be a timing one. if we wait 5-10 seconds before playing the video, the video works fine each time. – Crashalot Dec 09 '16 at 05:47
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.

Crashalot
  • 33,605
  • 61
  • 269
  • 439