2012-03-29 18 views
2

私のアプリでは、ウェブからビデオをダウンロードして再生します。ビデオをダウンロードして終了する前に再生する

私が使用しています:

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

をして私は、ファイルを取得データを保存するために:私はしたいダウンロードされたビデオから50%になったときに

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++; 

と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

答えて

0

あなたはそのようなビデオを再生することはできません。あなたのやり方をダウンロードしている場合は、完了するまで待つ必要があります。

あなたが望む方法について考えるなら、あなたはアプリケーションにビデオをストリーミングすることを話しています。それでは、ここに例があります:http://geobray.com/2010/03/26/live-tv-streaming-to-iphone-with-http/

とそれは私が推測するストリーミングファイルを作成する方法を示しています。

これは役に立ちます..

関連する問題