【问题标题】:how to cache or preload youtube video in uiwebview如何在 uiwebview 中缓存或预加载 youtube 视频
【发布时间】:2013-12-02 18:12:20
【问题描述】:

我有一个 youtube 视频,可以在我的应用程序的 uiwebview 中播放。在我使用该应用程序的大多数地区,手机信号都不是很好。

如何缓存 youtube 视频以便在播放视频时获得更好的性能?

【问题讨论】:

  • 使用 UIWebView,您几乎无法控制内容实际发生的情况。您可能会查看 YouTube 数据 API 以查看是否可以从那里缓存视频,但我怀疑 Google 是否希望您做您想做的事情。

标签: ios uiwebview youtube


【解决方案1】:

试试这个

我开始使用 NSURLConnection 下载视频文件 然后我实现接收到的数据委托方法,如下所示。

- (void)connection:(NSURLConnection *)aConnection didReceiveData:(NSData *)aData
{
    bytesFetched += aData.length;
    if( bytesFetched > kBytesRequiredBeforeStart && !hasCachedData )        // kBytesRequiredBeforeStart = 160000
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:kVideoURLCacheHasDataNotification object:self];
        hasCachedData = YES;
    }
    [self.fileHandle writeData:aData];          // this file handle is not closed until after the video has finished downloading
}

fileHandle 是这样创建的

- (NSFileHandle *)fileHandle
{
    if( fileHandle == nil )
    {
        NSError             * theError = nil;
        cachedURL = [[NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingFormat:@"/%@", kTempFileName]] retain];
        [[NSFileManager defaultManager] createFileAtPath:[self.cachedURL path] contents:nil attributes:nil];
        fileHandle = [[NSFileHandle fileHandleForWritingToURL:self.cachedURL error:&theError] retain];
        if( fileHandle == nil )
            [[NSNotificationCenter defaultCenter] postNotificationName:kVideoURLCacheErrorOccuredNotification object:self];
    }
    return fileHandle;
}

*我也有类似这样的完成委托处理方法*

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    if( hasCachedData == NO )
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:kVideoURLCacheHasDataNotification object:self];
        hasCachedData = YES;
    }
    hasFinishedCaching = YES;
    [[NSNotificationCenter defaultCenter] postNotificationName:kVideoURLCacheDidFinishLoadingNotification object:self];
}

然后我有一个方法来观察通知,如下所示

- (void)videoURLCacheHasDataNotification:(NSNotification *)aNotification
{
    [self.videoController play];
}

videoController 是如下创建的 MPMoviePlayerController 的一个实例,cachedURL 与上面定义的相同。

- (MPMoviePlayerController *)videoController
{
    if( videoController == nil )
    {
        NSURL       * theURL = self.videoURLCache.cachedURL;
        NSLog( @"Video URL = '%@'", theURL );
        videoController = [[MPMoviePlayerController alloc] initWithContentURL:theURL];
        videoController.shouldAutoplay = NO;
        [videoController setFullscreen:NO animated:NO];
        videoController.view.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleHeight;
        videoController.repeatMode = MPMovieRepeatModeOne;
        videoController.controlStyle = MPMovieControlStyleEmbedded;
        videoController.view.frame = self.videoView.bounds;
        [videoView addSubview:self.videoController.view];
    }
    NSParameterAssert(videoController != nil);
    return videoController;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-02
    • 2012-05-10
    • 2011-11-23
    • 2011-11-24
    • 2023-03-08
    • 2013-02-10
    • 1970-01-01
    • 2014-12-04
    相关资源
    最近更新 更多