AVKit – 视频片段仅循环 2 次
Posted
技术标签:
【中文标题】AVKit – 视频片段仅循环 2 次【英文标题】:AVKit – Video clips loop just 2 times 【发布时间】:2018-11-28 16:54:43 【问题描述】:我有四个视频剪辑,我想无休止地播放它们。但是我的AVQueuePlayer()
只循环了所有视频 2 次然后停止。
如何让他们玩不完?
这是我的代码:
import UIKit
import AVKit
class ViewController: UIViewController
private let player = AVQueuePlayer()
let clips = ["001", "002", "003", "004"]
private var token: NSKeyValueObservation?
var avPlayerView = AVPlayerViewController()
override func viewDidLoad()
super.viewDidLoad()
self.addAllVideosToPlayer()
override func viewDidAppear(_ animated: Bool)
super.viewDidAppear(true)
self.addAllVideosToPlayer()
func addAllVideosToPlayer()
avPlayerView.player = player
for clip in clips
let urlPath = Bundle.main.path(forResource: clip, ofType: "m4v")!
let url = URL(fileURLWithPath: urlPath)
let playerItem = AVPlayerItem(url: url)
player.insert(playerItem, after: player.items().last)
token = player.observe(\.currentItem) [weak self] player, _ in
if self!.player.items().count == 1 self?.addAllVideosToPlayer()
avPlayerView.showsPlaybackControls = false
player.volume = 0.0
present(avPlayerView, animated: true, completion: self.player.play() )
【问题讨论】:
不确定。但我认为他们玩了两次,因为你在viewDidLoad
和viewDidAppear
中两次调用addAllVideosToPlayer
。问题可能是永远不会触发以递归方式调用 addAllVideosToPlayer
的回调。
This 回答可以帮助你。唯一的问题是它位于Obj-C
。
【参考方案1】:
我解决了我的问题。我删除了viewDidLoad()
方法并将present()
函数放入viewDidAppear()
。
它是这样的:
override func viewDidAppear(_ animated: Bool)
super.viewDidAppear(true)
self.addAllVideosToPlayer()
present(avPlayerView, animated: true, completion: self.player.play() )
【讨论】:
以上是关于AVKit – 视频片段仅循环 2 次的主要内容,如果未能解决你的问题,请参考以下文章