How do you loop AVPlayer in Swift?

后端 未结 8 2021
后悔当初
后悔当初 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.

提交回复
热议问题