简体   繁体   中英

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.

我不确定是什么原因造成的,但对我有用的解决方案是重置 AVPlayerLayer 上的播放器

   avPlayerLayer = AVPlayerLayer(player: currentAvPlayer)

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

SWIFT 3

I had the same problem. Each time my app went into the background and then back into the foreground, my AVPlayer would disappear. Below is a simpler version of the code that works for me ... I needed to reset the player on UIApplicationWillEnterForeground. I can add more code if you need it, just let me know. It seems to be working for now. Cheers!

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

    }

}

最后我只是在播放器中重新加载视频文件。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM