AVPlayerLayer is empty after entering the background, being in the hidden controller and entering the foreground and displayed

I have an application in which I use AVPlayerLayer (used in a subclass of UIView, just like in the Apples AV Foundation Programming Guide). I have several view controllers that are stored in the view controller, which is responsible for the menu (using JASidePanels). The problem is this:

Everything works fine until the View controller with AVPlayers is hidden (another view appears) and the application enters the background, returns and returns to viewing. This causes the AVPlayerLayer to appear empty / transparent. The item is loaded in the same way as I can try to play it, and indeed, it plays, but the video is not visible.

What is the solution to this behavior (and what is its cause)? thanks in advance.

+6
source share
2 answers

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

avPlayerLayer = AVPlayerLayer(player: currentAvPlayer) 
+1
source

SWIFT 3

I had the same problem. Every time my application receded into the background and then returned to the foreground, my AVPlayer disappeared. Below is a simpler version of the code that works for me ... I needed to reset the player to UIApplicationWillEnterForeground. I can add more code if you need it, just let me know. It seems to be working now. Hooray!

 import UIKit import AVFoundation class ViewController: UIViewController { var avPlayer: AVPlayer! var avPlayerLayer: AVPlayerLayer! var paused: Bool = false override func viewDidLoad() { super.viewDidLoad() // Be sure to add your file to your app (not in the assets folder) and add it to your target let theURL = Bundle.main.url(forResource:"yourVideoFile", withExtension: "mp4") avPlayer = AVPlayer(url: theURL!) avPlayerLayer = AVPlayerLayer(player: avPlayer) avPlayerLayer.frame = view.layer.bounds avPlayerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill // .none is for looping videos if you uncomment "p: AVPlayerItem" below. // .pause is to pause at the end of the video and hold the last frame avPlayer.actionAtItemEnd = .pause view.layer.insertSublayer(avPlayerLayer, at: 0) NotificationCenter.default.addObserver(self, selector: #selector(playerItemDidReachEnd(notification:)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,object: avPlayer.currentItem) NotificationCenter.default.addObserver(self, selector: #selector(appMovingToForeground), name: Notification.Name.UIApplicationWillEnterForeground, object: nil) } override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) avPlayer.play() paused = false } func playerItemDidReachEnd(notification: Notification) { print("THE END") // uncomment the following line for a looping video // let p: AVPlayerItem = notification.object as! AVPlayerItem // p.seek(to: kCMTimeZero) } func appMovingToForeground() { print("App moved to foreground!") avPlayerLayer = AVPlayerLayer(player: avPlayer) view.layer.insertSublayer(avPlayerLayer, at: 0) // If you want your video or video loop to continue playing when app moves to foreground add: // avPlayer.play() // paused = false } } 
0
source

All Articles