Anna565
Anna565

Reputation: 735

AVURLAsset video buffering

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.

Upvotes: 2

Views: 2485

Answers (1)

Anna565
Anna565

Reputation: 735

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!

Upvotes: 5

Related Questions