gpt4 book ai didi

ios - Swift:在 avqueueplayer 中播放完毕后重播视频

转载 作者:行者123 更新时间:2023-11-30 11:16:17 24 4
gpt4 key购买 nike

我想在我的应用程序中用 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 播放器在播放完一次后不重新播放。

最佳答案

By looking at the documentation, I found that There is no direct way to loop through all the AVPlayerItem in AVQueuePlayer. You can use AVPlayerLooper to repeat one AVPlayerItem but can't repeat entire player list.

你能做的是:

在完成的方法中检查您自己的计数器,当它到达结束时,您可以使用函数removeAllItems()删除现有的播放器项目,并将[AVPlayerItem]重新分配给您的AVQueuePlayer实例。

希望有帮助。

关于ios - Swift:在 avqueueplayer 中播放完毕后重播视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51711870/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com