Swift:在avqueueplayer中播放完毕后重播视频
Posted
技术标签:
【中文标题】Swift:在avqueueplayer中播放完毕后重播视频【英文标题】:Swift: Replay video after finished playing in avqueueplayer 【发布时间】:2018-08-06 16:19:57 【问题描述】:我想在 swift 4 和 xcode 9 的应用程序中一个接一个地播放多个视频。我可以使用 avqueue 播放器来做到这一点。但是一旦播放器完成播放所有视频,则无法重播播放器。只是出现一个空白屏幕。
代码:
var selectedVideos : NSMutableArray = []
var videoPlayer: AVQueuePlayer!
var playerLayer: AVPlayerLayer!
var videoQueue: [AVPlayerItem] = []
var currentVideoTime: Double = 0
var previousVideoTime: Double = 0
var total_video_duration: Double = 0.0;
var total_video: Int = 0
@IBOutlet weak var video_container: UIView!
@IBOutlet weak var video_duration: UILabel!
override func viewDidLoad()
super.viewDidLoad()
setup_video_player()
setup_layer()
NotificationCenter.default.addObserver(self, selector: #selector(self.finishVideo), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)
func setup_video_player()
try! AVAudiosession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: [])
currentVideoTime = 0
previousVideoTime = 0
total_video_duration = 0
video_duration.text = "00:00"
videoPlayer = nil
videoQueue = []
total_video = selectedVideos.count
var playerItem: AVPlayerItem!
for index in selectedVideos
print("selected url: \(index as! URL)")
playerItem = AVPlayerItem(url: index as! URL)
videoQueue.append(playerItem)
videoPlayer = AVQueuePlayer(items: videoQueue)
for playerItem in self.videoPlayer.items()
let duration = playerItem.asset.duration
let seconds = CMTimeGetSeconds(duration)
total_video_duration = total_video_duration + seconds
let interval = CMTime(seconds: 1, preferredTimescale: 2)
timeObserver = videoPlayer.addPeriodicTimeObserver(forInterval: interval, queue: DispatchQueue.main, using: elapsedTime in
self.currentVideoTime = self.previousVideoTime + CMTimeGetSeconds(elapsedTime)
self.video_duration.text = self.video_duration(value: self.currentVideoTime)
)
print("finish setup video palyer")
play_video()
func setup_layer()
if(playerLayer != nil)
playerLayer.removeFromSuperlayer()
playerLayer = AVPlayerLayer(player: videoPlayer)
playerLayer.frame = video_container.bounds
video_container.layer.addSublayer(playerLayer)
print("finish setup layer ")
func play_video()
if videoPlayer == nil
return
videoPlayer.play()
videoPlayer.volume = 1.0
print("playing video")
@objc func finishVideo(_ notification: NSNotification)
previousVideoTime = currentVideoTime
print("current video time: \(currentVideoTime)")
print("total video duration: \(total_video_duration)")
if Int(currentVideoTime) != Int(total_video_duration)
return
play_video()
没有错误。但是无法理解为什么avqueue播放器在播放完一次后不重新播放。
【问题讨论】:
【参考方案1】:通过查看documentation,我发现没有直接的方法可以循环遍历AVQueuePlayer中的所有AVPlayerItem。您可以使用 AVPlayerLooper 重复一个 AVPlayerItem,但不能重复整个播放器列表。
你可以做的是:
在finished方法中检查您自己的计数器,当它到达结束时,您可以使用函数removeAllItems()删除现有的播放器项目并将[AVPlayerItem]重新分配给您的AVQueuePlayer实例。
希望对你有帮助。
【讨论】:
以上是关于Swift:在avqueueplayer中播放完毕后重播视频的主要内容,如果未能解决你的问题,请参考以下文章
在 AVQueuePlayer 中播放完最后一项后如何显示工作表视图?
AVMutableComposition - 只播放第一首曲目 (Swift)