你如何在Swift 4中循环AVPlayer?

时间:2018-04-01 20:37:49

标签: swift

我使用了以下代码:

NotificationCenter.default.addObserver(self,selector: #selector(ViewController.restartVideoFromBeginning),name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,object: nil)

@objc func restartVideoFromBeginning()  {


    let seconds : Int64 = 0
    let preferredTimeScale : Int32 = 1
    let seekTime : CMTime = CMTimeMake(seconds, preferredTimeScale)

    player!.seek(to: seekTime)

    player!.play()

}

但是restartVideoFromBeginning内的玩家有一个零值,有人知道为什么吗?

2 个答案:

答案 0 :(得分:5)

我试过这个并且有效

循环播放视频

NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: myPlayer.currentItem, queue: nil) { (_) in
        myPlayer.seek(to: kCMTimeZero)
        myPlayer.play()
    }
    myPlayerView.layer.addSublayer(avLayer)

答案 1 :(得分:0)

下面的Swift 4.2。我先来看看AVPlayerLooper。

        NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: player.currentItem, queue: nil) { (_) in
        player.seek(to: CMTime.zero)
        player.play()
    }