How to play videos from stream URL with some pre-buffering mechanism the way facebook did?

Viewed 631

In my app I have Timeline like facebook and want to implement autoplay the way facebook does. I am able to play a video whenever user stops scrolling on Video post inside UITableViewCell but it takes 5-10 seconds to play a video thats the problem.

I need expert guide how to pre - buffer videos from urls at-least 5 sec to give a user better experience or what is other way to play videos from URL instantly on 3g network. A user might have 100 videos post.

I can't find out which class buffers the videos AVURLAsset , AVPlayerItem or AVPlayer while playing .

I am loading AVURLAsset loadValuesAsynchronouslyForKeys and creating AVPlayerItems then Saving to NSDictionary for URL Key. Below is my code. For security reasons URL gets expired after 15 minutes.

   -(void)setContentURL:(NSURL *)contentURL
{

    if (contentURL)
    {
        [self.moviePlayer replaceCurrentItemWithPlayerItem:nil];
        [_activityIndicator startAnimating];
        __block  AVPlayerItem *playerItem=[_appDelegate.dictAVPlayerItems objectForKey:[contentURL.absoluteString stringByAppendingString:self.postIdOrBlogId]];
        if (!playerItem)
        {

            AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:contentURL options:nil];
            NSArray *keys = [NSArray arrayWithObject:@"playable"];

            [asset loadValuesAsynchronouslyForKeys:keys completionHandler:^()
             {
                 NSLog(@"keys %@", keys);
                 [self checkAssestStatus:asset];
                 if (asset==nil)return ;
                 playerItem = [AVPlayerItem playerItemWithAsset:asset];
                 [_appDelegate.dictAVPlayerItems setObject:playerItem forKey:[contentURL.absoluteString stringByAppendingString:self.postIdOrBlogId]];
                 dispatch_async(dispatch_get_main_queue(),
                                ^{
                                    [self addPlayerItem:playerItem isNewAsset:NO];

                                });


             }];
            _contentURL = contentURL;
        }

    else
        [self addPlayerItem:playerItem isNewAsset:YES];

    }
}


-(void)checkAssestStatus:(AVURLAsset*)asset
{
    NSError *error = nil;
    AVKeyValueStatus tracksStatus = [asset statusOfValueForKey:@"playable" error:&error];
    NSLog(@" AVURLAsset error = %@",error);
    if(!asset.isPlayable)
    {

        NSLog(@"assest is not playable");
        [self.activityIndicator stopAnimating];
        return;
    }
    switch (tracksStatus) {
        case AVKeyValueStatusLoaded:
        {
            NSLog(@"loaded");
        }
            break;
        case AVKeyValueStatusFailed:
        {
            if (error && (error.code == AVErrorUnknown
                                     || error.code == AVErrorFailedToLoadMediaData))
            {
                [_appDelegate.dictAVPlayerItems removeObjectForKey:[asset.URL.absoluteString stringByAppendingString:self.postIdOrBlogId]];
                NSLog(@"url Expired");
                asset=nil;
                [CommonTimelineAPI requestFreshURLFor:asset.URL.absoluteString withCompletionBlock:^(NSString *freshURL, NSError *error) {

                    if(freshURL)
                    {
                        NSURL* url=[NSURL URLWithString:freshURL];
                        if (url)
                        {
                            self.contentURL=url;

                        }
                    }
                }];
            }
        }
            break;
        case AVKeyValueStatusCancelled:
        {
            NSLog(@"cancelled");
        }
            break;
        case AVKeyValueStatusUnknown:
        {
            NSLog(@"unkonwn");
        }
            break;
        case AVKeyValueStatusLoading:
        {
            NSLog(@"Loading");

        }break;
    }


}
0 Answers
Related