你如何在Swift中循环AVPlayer?

Jon*_*ett 63 ios swift

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

你如何在Swift中循环AVPlayer?

numberOfLoops = -1仅适用于AVAudioPlayer

我确实需要它循环没有任何延迟/黑色闪光等.这就是为什么我没有使用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()
Run Code Online (Sandbox Code Playgroud)

Ale*_*kov 149

斯威夫特4

var player: AVPlayer!

...

NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: self.player.currentItem, queue: .main) { [weak self] _ in
    self?.player?.seek(to: CMTime.zero)
    self?.player?.play()
}
Run Code Online (Sandbox Code Playgroud)

斯威夫特3

NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: self.player.currentItem, queue: .main) { [weak self] _ in
    self?.player?.seek(to: kCMTimeZero)
    self?.player?.play()
}
Run Code Online (Sandbox Code Playgroud)

斯威夫特2

配置AVPlayerItem并创建播放器后:

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()
}
Run Code Online (Sandbox Code Playgroud)

  • 您可以添加`queue:.main`然后您不需要调度到主队列. (4认同)

Ben*_*ahl 28

@Christopher Pickslay的答案更新为Swift 4:

func loopVideo(videoPlayer: AVPlayer) {
    NotificationCenter.default.addObserver(forName: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil, queue: nil) { notification in
        videoPlayer.seek(to: CMTime.zero)
        videoPlayer.play()
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,正如我在下面提到的那样,如果你有多个玩家,请务必将对象指定为AVPlayer的玩家项目.


Chr*_*lay 26

或者,您可以使用基于块的NSNotificationCenter API:

func loopVideo(videoPlayer: AVPlayer) {
    NSNotificationCenter.defaultCenter().addObserverForName(AVPlayerItemDidPlayToEndTimeNotification, object: nil, queue: nil) { notification in
        videoPlayer.seekToTime(kCMTimeZero)
        videoPlayer.play()
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 更正@BenStahl的答案,你应该将`AVPlayerItem`指定为"对象"而不是'AVPlayer`.所以它应该是`videoPlayer.currentItem` (2认同)

Jon*_*ett 23

好的,我已经解决了.感谢Msencenb用Objective C答案指出了正确的方向.

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()

}
Run Code Online (Sandbox Code Playgroud)


Bri*_*ure 23

从iOS 10开始,无需使用通知来循环播放视频,只需使用AVPlayerLooper,就像这样:

let asset = AVAsset(url: videoURL)
let item = AVPlayerItem(asset: asset)
let player = AVQueuePlayer(playerItem: item)
videoLooper = AVPlayerLooper(player: player, templateItem: item)
Run Code Online (Sandbox Code Playgroud)

确保在函数范围之外创建此循环器,以防函数返回时停止.

  • 感谢上帝.`KVO`只是一种做事的hackish方式. (3认同)
  • @AlexanderFlenniken BrightFuture没有“ let videoLooper”的原因是因为正如他们所指出的,循环器需要保留在函数范围之外。因此,在类设置中的某个位置将存在一个“ var videoLooper”,而在此处的函数中您只是对其进行分配。如果您在设置功能内执行了“让”操作,但未将其分配给该功能外的属性,则在播放视频时将无法进行循环播放。 (2认同)

Swi*_*yle 12

斯威夫特 5.5:

func loopVideo(videoPlayer: AVPlayer) {
  NotificationCenter.default.addObserver(forName: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil, queue: nil) { notification in
    videoPlayer.seek(to: .zero)
    videoPlayer.play()
  }
}
Run Code Online (Sandbox Code Playgroud)


Jus*_*ion 8

我已经设法在swift 3中为OSX创建了一个无缝的视频循环.它应该可以在iOS上运行,并且在swift 2上也几乎没有修改.

var player : AVQueuePlayer!

// Looping video initial call
internal func selectVideoWithLoop(url : URL)
{
    let asset = AVAsset(url: url)
    player.pause()
    let playerItem1 = AVPlayerItem(asset: asset)
    let playerItem2 = AVPlayerItem(asset: asset)
    player.removeAllItems()
    player.replaceCurrentItem(with: playerItem1)
    player.insert(playerItem2, after: playerItem1)
    player.actionAtItemEnd = AVPlayerActionAtItemEnd.advance
    player.play()

    let selector = #selector(ViewController.playerItemDidReachEnd(notification:))
    let name = NSNotification.Name.AVPlayerItemDidPlayToEndTime
    // removing old observer and adding it again for sequential calls. 
    // Might not be necessary, but I like to unregister old notifications.
    NotificationCenter.default.removeObserver(self, name: name, object: nil)
    NotificationCenter.default.addObserver(self, selector: selector, name: name, object: nil)
}

// Loop video with threadmill pattern
// Called by NotificationCenter, don't call directly
func playerItemDidReachEnd(notification: Notification)
{
    let item = player.currentItem!
    player.remove(item)
    item.seek(to: kCMTimeZero)
    player.insert(item, after: nil)
}
Run Code Online (Sandbox Code Playgroud)

如果要更改视频,只需再次使用其他网址调用selectVideoWithLoop.


Akh*_*rma 5

Swift 3.0:

首先检查视频终点: -

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()
    }
Run Code Online (Sandbox Code Playgroud)