本文介绍了你如何在Swift中循环AVPlayer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单的问题我似乎无法找到某个原因的答案。

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

如何在Swift中循环AVPlayer?

How do you loop AVPlayer in Swift?

numberOfLoops = -1仅适用于AVAudioPlayer

numberOfLoops = -1 only works for AVAudioPlayer

我确实需要循环而没有任何延迟/黑闪等等。这就是为什么我不是使用MPMoviePlayerViewController。

I do need it to loop without any delay / black flash etc. That's why I'm not using MPMoviePlayerViewController.

感谢您的帮助。

代码:

    let url_1 = NSURL.fileURLWithPath(outputFilePath_1)

    let asset_1 = AVAsset.assetWithURL(url_1) as? AVAsset
    let playerItem_1 = AVPlayerItem(asset: asset_1)

    let player_1 = AVPlayer(playerItem: self.playerItem_1)

    let playerLayer_1 = AVPlayerLayer(player: self.player_1)

    playerLayer_1!.frame = self.view.frame

    self.view.layer.addSublayer(self.playerLayer_1)

    player_1!.play()


推荐答案

Swift 3

NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: self.player.currentItem, queue: .main) { _ in
    self.player?.seek(to: kCMTimeZero)
    self.player?.play()
}

Swift 2

配置AVPlayerItem并创建播放器后:

After AVPlayerItem is configured and player is created:

var player: AVPlayer!

...

// Invoke after player is created and AVPlayerItem is specified
NSNotificationCenter.defaultCenter().addObserver(self,
    selector: "playerItemDidReachEnd:",
    name: AVPlayerItemDidPlayToEndTimeNotification,
    object: self.player.currentItem)

...

func playerItemDidReachEnd(notification: NSNotification) {
    self.player.seekToTime(kCMTimeZero)
    self.player.play()
}

这篇关于你如何在Swift中循环AVPlayer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 11:24