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

时间:2018-08-06 16:19:57

标签: ios swift xcode avplayer avqueueplayer

我想在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 个答案:

答案 0 :(得分:0)

  

通过查看documentation,我发现没有直接方法可以遍历AVQueuePlayer中的所有AVPlayerItem。您可以使用 AVPlayerLooper 重复一个AVPlayerItem ,但不能重复整个播放器列表。

您可以做的是:

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

希望有帮助。