2

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, then previous and next player button enable , and play when they click.

Here is my code;

         var commmentQueuePlayer = AVQueuePlayer()
         var items = [AVPlayerItem]()
         if let player = comPlayerControl {

            let videoURL: String = "http://cdnapi.kaltura.com/p/11/sp/11/playManifest/entryId/0_6swapj1k/format/applehttp/protocol/http/a.m3u8"
            let firstItemURL: String = "http://cdnapi.kaltura.com/p/11/sp/11/playManifest/entryId/0_2p3957qy/format/applehttp/protocol/http/a.m3u8"
            let secondItemURL: String = "http://cdnapi.kaltura.com/p/11/sp/11/playManifest/entryId/0_buy5xjol/format/applehttp/protocol/http/a.m3u8"


            let firstItem = AVPlayerItem(URL: NSURL(string: firstItemURL)! )
            let secondItem = AVPlayerItem(URL: NSURL(string: secondItemURL)! )
            let playerItem = AVPlayerItem(URL: NSURL(string: videoURL)! )

            items = [firstItem,secondItem,playerItem]
            commmentQueuePlayer = AVQueuePlayer(items: items)

            player.player = commmentQueuePlayer
            player.view.frame = videoCell.frame
            player.view.sizeToFit()
            player.showsPlaybackControls = true
            videoCell.addSubview(player.view)
           // comPlayerControl.player?.play()
        }

What is happening here is once playing start second one. like a Queue.

Plese let me know is it good to go with AVQueuePlayerController or AVPlayerController.

Please help me anyone.I Google it, unfortunately, I could not get the solution.

I want to know I can trigger Next previous player icon

enter image description here

Please suggest.

Piraba
  • 6,974
  • 17
  • 85
  • 135
  • so you want to play the next/previous video on the it's respective button click? didn't get this portion what you are trying to say `What is happening here is once playing start second one. like a Queue.` – HardikDG Apr 02 '16 at 03:51
  • Yes. Just imagine, I have a list of videos preview in the tableview , once user table open in the full-screen mode & play. Once play finished, don't want to go back to select next video , in that case user click next button it will play next video, if the user click previous, if there are previous item, then it play – Piraba Apr 02 '16 at 08:09
  • 1
    It is difficult to believe that no way to enable these buttons. I did not found quick way how to do. What I found that recommending to write own customs video player. As reference can be this: http://binarymosaic.com/custom-video-player-for-ios-with-avfoundation/ – Ramis Apr 02 '16 at 11:14
  • Thanks for the response. I have a question about calling custom button on top of the video after playing finished. http://stackoverflow.com/questions/33653805/avplayer-avplayerlayer-not-appearing‌​-in-subview-using-auto-layout – Piraba Apr 02 '16 at 15:59
  • So. You do not want to enable default player next previous buttons, but you want add additional buttons on top of player. Is it right? – Ramis Apr 03 '16 at 07:53
  • @Ramis is it possible to enable default player next previous buttons? I checked. dead tried, i couldn't find the solution – Piraba Apr 04 '16 at 04:56
  • @Piraba I could not found solution how to enable default buttons. – Ramis Apr 04 '16 at 05:41

2 Answers2

1

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.
Ramis
  • 13,985
  • 7
  • 81
  • 100
  • Thanks for the response. Could please tell me how to call methof. I did like this `let vc = ViewController() vc.setupPlayer()` . Here `ERROR: 181: timed out after 0.012s (612 613); mMajorChangePending=0` – Piraba Apr 04 '16 at 04:54
  • Are you getting this error all the time? Can it bet, that server is not responding? I see that people getting this error when they are playing sound. https://forums.developer.apple.com/thread/19902 – Ramis Apr 04 '16 at 05:45
0

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.

Community
  • 1
  • 1
Daniel Ormeño
  • 2,743
  • 2
  • 25
  • 30
  • My requirement is, the user may select previous or next video once selected item a play completed. In this solution user can'y click previous option. Could you please tell me how i can tackle previous or next item from this. will it work default AVPlayerController's next previous ? – Piraba Apr 04 '16 at 06:14
  • 1
    The queuePlayer will automatically play the next video once a video finishes playing, my understanding is that the buttons you are trying to use are for fast forwarding and rewinding the media asset, which might not be available for streamed videos, a suggested [here](https://forums.developer.apple.com/thread/21629). You might need to add a custom controller for your player. The AVQueuePlayer class does not have a native previousItem option, as items from the queue are removed after they finish playing. You can also use NSNotifications like [this](http://stackoverflow.com/a/12605261/5229157) – Daniel Ormeño Apr 04 '16 at 06:21