2

I'd like to buffer a video using AVPlayer. I play one video and I'd like to not have delay when the second video starts. One idea is to use AVURLAsset with this code:

     AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:urlNext options:nil];
        NSArray *keys = [NSArray arrayWithObject:@"playable"];
        [asset loadValuesAsynchronouslyForKeys:keys completionHandler:^()
         {
             dispatch_async(dispatch_get_main_queue(), ^
                            {
                                AVPlayerItem *nextVideo = [[AVPlayerItem alloc] initWithAsset:asset];
                            });
         }];

In this way the idea is to pre-load the next video and when the first video stops, play it. But still now this don't works and the next video don't appears. Do you know if my idea is correct and what is the error? Or do you have another idea to use AVPlayer and to have buffer? Thank you.

Anna565
  • 735
  • 2
  • 10
  • 21
  • maybe if you load your avplayer with a list of items it will cache the next one. AVPlayer start buffering a video when it start playing it. http://developer.apple.com/library/ios/#documentation/AVFoundation/Reference/AVPlayer_Class/Reference/Reference.html look at prerollAtRate:completionHandler – Nicolas Manzini Aug 01 '13 at 08:03
  • Can you can make an example? – Anna565 Aug 01 '13 at 08:05
  • actually look at AVQueuePlayer it extend AVPlayer to play a list of streaming maybe it's what you want – Nicolas Manzini Aug 01 '13 at 08:11
  • Yes thank you, my ispiration is this: http://stackoverflow.com/questions/14941461/avqueueplayer-playback-without-gap-and-freeze but using it I still have a delay between the videos so I try to find similar solutions – Anna565 Aug 01 '13 at 08:13
  • good luck I don't much more about it. – Nicolas Manzini Aug 01 '13 at 08:17
  • Ok, I'll wait for other answers – Anna565 Aug 01 '13 at 08:22
  • It is too old but for other developers, Go with a loaded state instead of directly calling the next video. loading is also there so once it loaded the content in Asset then its good to display on player. Then it will play immediately with no delay. var error: NSError? = nil let status = self.asset.statusOfValue(forKey: "playable", error: &error) switch status { case .loaded: – Rahul Verma Aug 24 '23 at 05:33

1 Answers1

5

For the moment, I found a solution not so elegant but it works in the simulator (I have to test it in the device). I have used the idea number 1 of How to reduce iOS AVPlayer start delay and I have created another AVPlayer. It simply store the next video (I play and pause it).

    AVPlayerItem *playerItemNext = [AVPlayerItem playerItemWithURL:urlnext];
    AVPlayer *videoPlayerNext = [AVPlayer playerWithPlayerItem:playerItemNext];
    [videoPlayerNext play];
    [videoPlayerNext pause]; 

And when I have to really play this next video I simply do:

     videoPlayer = videoPlayerNext;

Where videoPlayer is my player. Obviously better solutions are welcome!

Community
  • 1
  • 1
Anna565
  • 735
  • 2
  • 10
  • 21