简体   繁体   English

TVOS 中的连续循环视频?

[英]Continuous Looping Video in TVOS?

I'm just getting started with TVOS and was wondering if anyone has addressed looping in TVOS / TVJS / TVML.我刚刚开始使用 TVOS,想知道是否有人解决了 TVOS/TVJS/TVML 中的循环问题。 I'm using this tutorial to create a simple interface for playing videos.我正在使用本教程创建一个用于播放视频的简单界面。 My wrinkle is that I want these videos to play in a continuous seamless loop - basically a moving screensaver type effect - see example videos at art.chrisbaily.com .我的问题是我希望这些视频以连续无缝循环播放 - 基本上是一种移动的屏幕保护程序类型效果 - 请参阅art.chrisbaily.com 上的示例视频。

Is there a simple way to do this, or do I need to build some kind of event listener to do the looping manually?有没有一种简单的方法可以做到这一点,或者我是否需要构建某种事件侦听器来手动执行循环? I'd like the videos to be fairly hi res, and the videos would be somewhere between 1 and 3 minutes.我希望视频具有相当高的分辨率,并且视频长度在 1 到 3 分钟之间。

I was looking for an answer for this question as well.我也在寻找这个问题的答案。 And found that the only way this would be possible is to create an event listener and adding a duplicate media item to the playlist.并发现唯一可能的方法是创建一个事件侦听器并将重复的媒体项添加到播放列表中。 But honestly it is not that hard, given if you followed the tutorial that you listed in your post.但老实说,这并不难,如果您按照您在帖子中列出的教程进行操作。

So the code would be something like所以代码会是这样的

player.playlist = playlist;
player.playlist.push(mediaItem);
//Again push the same type of media item in playlist so now you have two of the same.
player.playlist.push(mediaItem); 
player.present();

This will make sure that once your first video ends the second one starts playing which is essentially a loop.这将确保一旦您的第一个视频结束,第二个视频就会开始播放,这本质上是一个循环。 Then for the third one and on you implement an event listener using " mediaItemWillChange " property.然后对于第三个,然后使用“ mediaItemWillChange ”属性实现一个事件侦听器。 This will make sure that once a video ends a new copy of the same video is added to the playlist.这将确保在视频结束后将同一视频的新副本添加到播放列表中。

player.addEventListener("mediaItemWillChange", function(e) { 
   player.playlist.push(mediaItem);
});

Just put the event listener before you start the player using只需在使用开始播放器之前放置事件侦听器

player.present();

Note this question and idea of this sort was already asked/provided on Apple's discussion board.请注意,Apple 的讨论板上已经提出/提供了此类问题和想法。 I merely took the idea and had implemented it in my own project and now knowing that it works I am posting the solution.我只是接受了这个想法并在我自己的项目中实现了它,现在知道它有效,我发布了解决方案。 I found that if I pop the first video out of the playlist and add then push a new on the playlist as mentioned in the linked thread below, my videos were not looping.我发现如果我从播放列表中弹出第一个视频并添加然后在播放列表上推送一个新视频,如下面的链接线程中所述,我的视频不会循环播放。 Below is the link to the original post.下面是原帖的链接。

How to repeat video with TVJS Player?如何使用 TVJS 播放器重复播放视频?

You could also set repeatMode on the playlist object您还可以在播放列表对象上设置 repeatMode

player.playlist.repeatMode = 1;
  • 0 = no repeat 0 = 不重复
  • 1 = repeat all items in playlist 1 = 重复播放列表中的所有项目
  • 2 = repeat current item 2 = 重复当前项目

There's really no need to push a second media item onto the.真的没有必要将第二个媒体项目推到 . Simply listen for the media to reach its end, then set the seek time back to zero.只需聆听媒体到达终点,然后将寻道时间设置回零。 Here's the code.这是代码。

Play the media.播放媒体。

    private func playVideo(name: String) {

    guard name != "" else {
        return
    }

    let bundle = NSBundle(forClass:object_getClass(self))
    let path = bundle.pathForResource(name, ofType:"mp4")
    let url = NSURL.fileURLWithPath(path!)
    self.avPlayer = AVPlayer(URL: url)

    if (self.avPlayerLayer == nil) {
        self.avPlayerLayer = AVPlayerLayer(player: self.avPlayer)
        self.avPlayerLayer.frame = previewViews[1].frame
        previewViews[1].layer.addSublayer(self.avPlayerLayer)

        avPlayer.actionAtItemEnd = .None

        //Listen for AVPlayerItemDidPlayToEndTimeNotification
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerItemDidReachEnd:", name: AVPlayerItemDidPlayToEndTimeNotification, object: self.avPlayer.currentItem)
    }

    avPlayer.play()

}

Play Again再玩一次

    func playerItemDidReachEnd(notification: NSNotification) {
        let item = notification.object as? AVPlayerItem
        item?.seekToTime(kCMTimeZero)
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM