Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show paused video instead of black screen when starting AVPlayer

I am able to successfully create a player but was annoyed with the initial black screen. I decided to overlay a UIImageView and hide it once the player started. This worked, but I didn't want the hassle of creating and maintaining images for all my videos.

I was able to achieve the exact results I wanted by playing and immediately pausing the player after instantiating it. The only issue was that sometimes the state of the player was getting recorded incorrectly, so when I went to start the player again, the status was listed as already "playing" even though the player was paused.

I starting looking into using AVPlayerItem seekToTime but haven't found any feasible solutions. Is there a "non hacky" way of achieving this?

like image 728
django-d Avatar asked Oct 02 '14 03:10

django-d


People also ask

How to fix black screen when playing videos on PC?

If the black screen is faced while playing videos, you need to click the file double to clear the cache, which will resolve your issue. This is another process that will unload your system completely. You need to access the RUN window and type TEMP. Hit enter, and you will get access to the hidden folder.

Why does my YouTube go black when I Play videos?

But alas, this very software is also another cause of the black screen problem. Thirdly, a prevalent cause of the black screen issue is the Browse Cache. The Browse Cache files tend to clog up the YouTube videos so much so that the video (s) is/are prevented from outputting.

Why are YouTube videos not playing on my computer?

The Browse Cache files tend to clog up the YouTube videos so much so that the video (s) is/are prevented from outputting. This is the term; causes the error. Another potential cause of the black screen error is the Browser Setting which causes the black screen error.

What to do when your computer is stuck playing videos?

If the computer is stuck, you need to press CTRL + ALT + DEL to access the main screen. Restart the PC from there. The video file has not been copied properly. Unplug all the USB devices, which will check that if any external device is responsible. Unplug all the cables if the above steps do not work.


1 Answers

If you're using an AVPlayerViewController, this is a perfect use of the player controller's contentOverlayView property. This is a UIView between the player layer and the controls exposed exactly for this purpose:

First, create the screenshot:

let asset = AVAsset(URL: URL(string: "")!) // link to some video
let imageGenerator = AVAssetImageGenerator(asset: asset)
let screenshotTime = CMTime(seconds: 1, preferredTimescale: 1)
if let imageRef = try? imageGenerator.copyCGImageAtTime(screenshotTime, actualTime: nil) {

    let image = UIImage(CGImage: imageRef)

    // see part 2 below
}

Now, add the image as a subview of the contentOverlayView in the player controller:

// in the same try block

let imageView = UIImageView(image: image)
let playerVC = AVPlayerViewController()
let playerItem = AVPlayerItem(asset: asset)
playerVC.player = AVPlayer(playerItem: playerItem)

self.presentViewController(playerVC, animated: true) { 
    playerVC.contentOverlayView?.addSubview(imageView)
    // adjust the frame of your imageView to fit on the player controller's contentOverlayView
}

Then, remove the imageView subview when the player starts playing the asset, or when buffering completes.

like image 149
JAL Avatar answered Sep 26 '22 22:09

JAL