首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AVPlayerLooper不会循环浏览多个视频

AVPlayerLooper不会循环浏览多个视频
EN

Stack Overflow用户
提问于 2018-03-07 09:33:04
回答 1查看 3.1K关注 0票数 6

我创建了一个AVPlayerView,用于使用AVPlayerLooper不间断地播放4个视频。问题是:AVPlayerView只显示一个环形视频。但我需要播放一个序列(4个视频)。

AVPlayerLooper 如何使播放所有4个视频,并在之后循环播放

代码语言:javascript
复制
import Cocoa
import AVFoundation

class ViewController: NSViewController {

    @IBOutlet weak var viewOne: AVPlayerView!
    var playerLooper: AVPlayerLooper? = nil

    override func viewDidLoad() {
        super.viewDidLoad()
    
        let videos = (1...4).map { _ in
            Bundle.main.urls(forResourcesWithExtension: "mov", 
                                          subdirectory: nil)![Int(arc4random_uniform(UInt32(4)))]
        }
    
        viewOne.player = AVQueuePlayer(url: videos[Int(arc4random_uniform(UInt32(4)))])
        let item = AVPlayerItem(url: videos[Int(arc4random_uniform(UInt32(4)))])
        playerLooper = AVPlayerLooper(player: viewOne.player as! AVQueuePlayer, templateItem: item)
        viewOne.player?.actionAtItemEnd = .advance
        viewOne.controlsStyle = .none

        NotificationCenter.default.addObserver(self, 
                                     selector: #selector(loopSequence), 
                                         name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, 
                                       object: viewOne.player?.currentItem)
    
        func loopSequence() {
            self.viewOne.player?.pause()
            self.viewOne.player?.currentItem?.seek(to: kCMTimeZero, completionHandler: nil)
            self.viewOne.player?.play()
        }
        loopSequence()
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-07 17:32:40

在找到先前被回复的解决方案并将其张贴在评论中之后,@andy要求我将其作为解决方案发布。HERE是哈里什解决他的问题的前一个答案。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49148442

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档