Swift 2 AVPlayer - Play Next video Previous Video

后端 未结 2 2040
逝去的感伤
逝去的感伤 2021-01-23 03:15

I am delving this part more than one day. Please suggest any idea.

I have to accomplish to play video.I have a list of videos. If the user makes it full-screen video, th

2条回答
  •  灰色年华
    2021-01-23 03:38

    I'd recommend Using the AVQueuePlayer. The queue player will automatically play the next item in the queue after the current item finishes playing. One think to keep in mind is that after an AVPlayerItem finishes playing, it will get removed from the queue, so if you are to go backwards you will have to either reset the queue or manually add new items to the queue here is an implementation of this.

    In Swift reseting the queue should look something like this

    func resetVideoQueue(video: string){
    
        if let videos = Videos {
    
            var queue: [AVPlayerItem] = []
    
            switch video {
            case "vid1":
                queue.append(AVPlayerItem(URL: NSURL(string: videos["vid1"])!))
                queue.append(AVPlayerItem(URL: NSURL(string: videos["vid2"])!))
                queue.append(AVPlayerItem(URL: NSURL(string: videos["vid3"])!))
                queue.append(AVPlayerItem(URL: NSURL(string: videos["vid4"])!))
                break
            case "vid2":
                queue.append(AVPlayerItem(URL: NSURL(string: videos["vid2"])!))
                queue.append(AVPlayerItem(URL: NSURL(string: videos["vid3"])!))
                queue.append(AVPlayerItem(URL: NSURL(string: videos["vid4"])!))
                break
            case "vid3":
                queue.append(AVPlayerItem(URL: NSURL(string: videos["vid3"])!))
                queue.append(AVPlayerItem(URL: NSURL(string: videos["vid4"])!))
                break
            case "vid4":
                queue.append(AVPlayerItem(URL: NSURL(string: videos["vid4"])!))
                break
            }
    
            setVideoPlayer(queue)
        }
    }
    
    
    internal func setVideoPlayer(queue: [AVPlayerItem]) {
        queuePlayer = AVQueuePlayer(items: queue)       
        videoPlayerViewController.player = queuePlayer
        presentViewController(videoPlayerViewController, animated: true) {
            self.videoPlayerViewController.player?.play()
        }
    }
    

    To move to the next item use the advanceToNextItem() method as suggested by MrHaze.

提交回复
热议问题