Swift 2 AVPlayer - Play Next video Previous Video

后端 未结 2 2022
逝去的感伤
逝去的感伤 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.

    0 讨论(0)
  • 2021-01-23 03:47

    I did create a demo project where it is shown how to put a custom buttons on top of AVPlayer and they responds to clicks.

    What was done:

    • Xib (uses auto layout) file created which hold custom buttons like next, replay and others.
    • Buttons were connected to the source which prints what needs to do. Like: play next or replay.
    • Was added observer which observe when video is finished to play.
    • When video is finished to play xib file with buttons will be shown.
    0 讨论(0)
提交回复
热议问题