How do you loop AVPlayer in Swift?

后端 未结 8 2015
后悔当初
后悔当初 2020-12-02 15:15

Simple question I can\'t seem to find an answer to for some reason.

How do you loop AVPlayer in Swift?

numberOfLoops = -1 only works for AVAudioPlayer

<
相关标签:
8条回答
  • 2020-12-02 15:55

    Starting from iOS 10, there's no need to use notifications to loop a video, simply use a AVPlayerLooper, just like this:

    let asset = AVAsset(url: videoURL)
    let item = AVPlayerItem(asset: asset)
    let player = AVQueuePlayer(playerItem: item)
    videoLooper = AVPlayerLooper(player: player, templateItem: item)
    

    make sure this looper is created outside of a function scope, in case it stops when function returns.

    0 讨论(0)
  • 2020-12-02 16:02

    Swift 3.0:

    First check for video end point:-

    NotificationCenter.default.addObserver(self, selector: #selector(LoginViewController.playerItemDidReachEnd), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: self.player?.currentItem)
    
    
    
    func videoDidReachEnd() {
    
            //now use seek to make current playback time to the specified time in this case (O)
            let duration : Int64 = 0 (can be Int32 also)
            let preferredTimeScale : Int32 = 1
            let seekTime : CMTime = CMTimeMake(duration, preferredTimeScale)
            player!.seek(to: seekTime)
            player!.play()
        }
    
    0 讨论(0)
提交回复
热议问题