使用AVPlayerLooper时,“由于内存问题而终止”

时间:2019-01-23 09:17:58

标签: ios swift avplayer avplayerlooper

使用AVPlayerLooper无限循环播放视频时,我的应用程序崩溃并显示“由于内存问题终止”。该问题仅在较旧的设备上发生(在iPhone 6S和更高版本上则不发生),并且循环非常短(少于1秒)。

我尝试使用AVPlayer并观察AVPlayerItemDidPlayToEndTime,然后尝试手动开始,但是此解决方案导致每个循环之间出现短暂的中断/打ic,这会导致我的特定需求出现时序问题。 在致电seek(to: CMTime.zero)之前暂停并不能解决问题。

这是我的Looper课:

class Looper: NSObject {
    private var playerItem: AVPlayerItem!
    private var player: AVQueuePlayer!
    private let playerLayer = AVPlayerLayer()
    private var playerLooper: AVPlayerLooper!
    private var isPaused: Bool = false {
        didSet {
            isPaused ? player.pause() : player.play()
        }
    }
    weak var delegate: LooperDelegate?

    required init(videoURL: URL) {
        super.init()
        let asset = AVURLAsset(url: videoURL, options: ["AVURLAssetPreferPreciseDurationAndTimingKey" : true])
        playerItem = AVPlayerItem(asset: asset)
        player = AVQueuePlayer(playerItem: playerItem)
        playerLayer.player = player
        playerLooper = AVPlayerLooper(player: player, templateItem: playerItem)

        player.addObserver(self, forKeyPath: #keyPath(AVPlayer.status), options: [.old, .new], context: nil)
        player.addObserver(self, forKeyPath: #keyPath(AVPlayer.timeControlStatus), options: [.old, .new], context: nil)
        playerLooper.addObserver(self, forKeyPath: #keyPath(AVPlayerLooper.status), options: [.old, .new], context: nil)
    }

    deinit {
        player.removeObserver(self, forKeyPath: #keyPath(AVPlayer.status))
        player.removeObserver(self, forKeyPath: #keyPath(AVPlayer.timeControlStatus))
        playerLooper.removeObserver(self, forKeyPath: #keyPath(AVPlayerLooper.status))
    }

    func add(in parentLayer: CALayer) {
        playerLayer.frame = parentLayer.bounds
        parentLayer.addSublayer(playerLayer)
    }

    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if object as AnyObject? === self.player &&
            keyPath == #keyPath(AVPlayer.status),
            let statusNumber = change?[.newKey] as? NSNumber,
            AVPlayer.Status(rawValue: statusNumber.intValue)! == .readyToPlay {
            self.delegate?.isReadyToPlay(duration: playerItem.asset.duration.seconds)
            print("Duration: \(playerItem.asset.duration.seconds)")

        } else if object as AnyObject? === self.player &&
            keyPath == #keyPath(AVPlayer.timeControlStatus),
            let oldKey = change?[.oldKey] as? NSNumber,
            let newKey = change?[.newKey] as? NSNumber,
            oldKey != 2 && newKey == 2 {
            self.delegate?.isLooping()
            print("isLooping")

        } else if object as AnyObject? === self.playerLooper &&
            keyPath == #keyPath(AVPlayerLooper.status),
            let statusNumber = change?[.newKey] as? NSNumber {
            if AVPlayerLooper.Status(rawValue: statusNumber.intValue)! == .ready {
                self.delegate?.isReadyForDisplay()
                print("isReadyForDisplay")
            }
        }
    }
}

0 个答案:

没有答案