Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVPlayerLayer blank after entering background while being in a hidden controller and entering foreground and being shown

I have an App in which I use AVPlayerLayer (used within a subclass of UIView just like Apples AV Foundation Programming Guide). Ihave a couple of view controllers witch are held in the View Controller that is resonsible for the menu (JASidePanels are used). The problem is as follows:

Everything works ok until the view controller with the AVPlayers does'nt get hidden (some other view is presented) and the the app enters background, gets back again and returns to the view. This Causes the AVPlayerLayer to display blank/transparent. The item is loaded as well as i can try playing it and indeed it is played but no video is seen.

What is the solution to this behaviour (and what is the cause of it)? Thx in advance.

like image 628
okipol Avatar asked Feb 12 '14 18:02

okipol


2 Answers

player?.seek(to: .zero) triggers AVPlayerLayer to render preview.
To smoothly update and pause the video on closing/opening the app in my case I've added the following:

private func setupObservers() {
    NotificationCenter.default.addObserver(self, selector: #selector(willResignActive), name: UIApplication.willResignActiveNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(didBecomeActive), name: UIApplication.didBecomeActiveNotification, object: nil)
}

@objc
private func willResignActive() {
    player?.pause()
}

@objc
private func didBecomeActive() {
    player?.seek(to: .zero)
}
like image 138
smartwolf Avatar answered Nov 07 '22 04:11

smartwolf


I'm not sure what causes it but a solution that worked for me was to reset the player on the AVPlayerLayer

   avPlayerLayer = AVPlayerLayer(player: currentAvPlayer)
like image 37
Ryan Detzel Avatar answered Nov 07 '22 05:11

Ryan Detzel