AVPlayerLayer在进入后台时处于隐藏控制器并进入前景并显示后为空白

时间:2014-02-12 18:52:55

标签: ios calayer avplayer avplayerlayer

我有一个应用程序,其中我使用AVPlayerLayer(在UIView的子类中使用,就像Apples AV Foundation Programming Guide一样)。我在视图控制器中保存了几个视图控制器,它们对于菜单是可靠的(使用JASidePanels)。问题如下:

一切正常,直到具有AVPlayers的视图控制器不被隐藏(呈现其他视图)并且应用程序进入背景,再次返回并返回到视图。这会导致AVPlayerLayer显示空白/透明。该项目已加载,我可以尝试播放它,确实播放但没有看到视频。

这种行为的解决方案是什么(原因是什么)? Thx提前。

4 个答案:

答案 0 :(得分:1)

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

   avPlayerLayer = AVPlayerLayer(player: currentAvPlayer)

答案 1 :(得分:1)

player?.seek(to: .zero)触发AVPlayerLayer来呈现预览。
为了在关闭/打开应用程序时顺利更新和暂停视频,我添加了以下内容:

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

答案 2 :(得分:0)

SWIFT 3

我遇到了同样的问题。每当我的应用程序进入后台然后回到前台时,我的AVPlayer就会消失。下面是一个更简单的代码版本,对我有用......我需要在UIApplicationWillEnterForeground上重置播放器。如果您需要,我可以添加更多代码,请告诉我。它似乎现在正在发挥作用。干杯!

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

    }

}

答案 3 :(得分:0)

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