2012-03-29 113 views
2

在我的应用程序中,我从网上下载了一个视频,我想播放下载。在完成下载前播放视频

我使用:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data; 

救我到一个文件中的数据:

if (currentBytes == 0) { 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *fileName = [NSString stringWithFormat:@"%@.mp4", [[paths objectAtIndex:0] stringByAppendingPathComponent:@"1"]]; 
     [[NSFileManager defaultManager] createFileAtPath:fileName contents:data attributes:nil]; 
     handle = [[NSFileHandle fileHandleForUpdatingAtPath:fileName] retain]; 
     [handle seekToEndOfFile]; 
    }else { 
     if (!data) { 
      NSLog(@""); 
     } 
     [handle writeData:data]; 
    } 
    currentBytes++; 

,然后当我从被下载我想将影片的观众,50%开始玩AVPlayer吧:

if ((percentComplete > 50)&&(flag == NO)) { 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *fileName = [NSString stringWithFormat:@"%@.mp4", [[paths objectAtIndex:0] stringByAppendingPathComponent:@"1"]]; 

    audioPlayer = [[AVPlayer playerWithURL:[NSURL fileURLWithPath:fileName]] retain]; 
    avPlayerLayer = [[AVPlayerLayer playerLayerWithPlayer:audioPlayer] retain]; 
    [avPlayerLayer setFrame:self.view.bounds]; 

    [audioPlayer play]; 

    [[self.view layer] addSublayer:avPlayerLayer]; 


    flag = YES; 
} 

任何想法为什么它不会工作?

+0

AvPlayer是否可以在文件播放前检查文件?也许AvPlayer期待在文件末尾有一些特别的东西。 - 只是想法。 – Max 2012-03-29 11:54:13

回答

0

你不能播放这样的视频。如果你正在下载你的方式,你需要等到完成。

如果我们考虑您想要的方式,您正在讨论将视频流式传输到您的应用程序。所以要做到这一点,这里是一个例子:http://geobray.com/2010/03/26/live-tv-streaming-to-iphone-with-http/

它显示了如何创建流文件我猜。

希望这会有所帮助..

相关问题