如何使用Facebook的方式使用一些预缓冲机制从流URL播放视频?

时间:2016-03-21 13:23:24

标签: ios objective-c avplayer

在我的应用程序中,我有像Facebook这样的时间轴,并希望以facebook的方式实现自动播放。每当用户停止在UITableViewCell内的视频帖子上滚动时,我就可以播放视频,但播放问题的视频需要5-10秒。

我需要专家指导如何在至少5秒内预先缓冲来自网址的视频,以便为用户提供更好的体验,或者在3g网络上立即从URL播放视频的其他方式。用户可能会发布100个视频。

我无法找到哪个类在播放时缓冲视频AVURLAsset,AVPlayerItem或AVPlayer。

我正在加载AVURLAsset loadValuesAsynchronouslyForKeys并创建AVPlayerItems然后保存到NSDictionary for URL Key。 以下是我的代码。出于安全原因,URL在15分钟后过期。

   -(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 个答案:

没有答案