How to play a video from a stream url using some pre-buffering mechanism like facebook did?

In my application, I have a Timeline like facebook and you want to implement autostart like facebook does. I can play the video when the user stops scrolling the video recording in the UITableViewCell, but for the video to play, this problem takes 5-10 seconds.

I need expert guidance on how to pre-buffer videos from URLs for at least 5 seconds to give the user a better experience or something else to play video from a URL instantly on a 3g network. A user can have 100 videos.

I can’t find out which class buffers AVURLAsset, AVPlayerItem or AVPlayer video during the game.

I load AVURLAsset loadValuesAsynchronouslyForKeys and create AVPlayerItems and then save the NSDictionary for the URL key. Below is my code. For security reasons, the URL expires 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; } } 
+5
source share

Source: https://habr.com/ru/post/1245486/


All Articles