How do you loop AVPlayer in Swift?

后端 未结 8 2043
后悔当初
后悔当初 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:53

    OK I have worked it out. Thanks Msencenb for pointing me in the right direction with an Objective C answer.

    player_1?.actionAtItemEnd = .None
    
    //set a listener for when the video ends   
    NSNotificationCenter.defaultCenter().addObserver(self,
            selector: "restartVideoFromBeginning",
            name: AVPlayerItemDidPlayToEndTimeNotification,
            object: player_1?.currentItem)
    
    
    //function to restart the video
    func restartVideoFromBeginning()  {
    
        //create a CMTime for zero seconds so we can go back to the beginning
        let seconds : Int64 = 0
        let preferredTimeScale : Int32 = 1
        let seekTime : CMTime = CMTimeMake(seconds, preferredTimeScale)
    
        player_1!.seekToTime(seekTime)
    
        player_1!.play()
    
    }
    

提交回复
热议问题