2012-05-09 17 views
0

デバイスにthis mp4 clipをダウンロードする接続を設​​定しましたが、次のデリゲート機能を使用してデータを「ストリーミング」形式で保存しています。mp4ファイルをダウンロードする際に問題が発生する

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"BuckBunny.mp4"]; 

    [data writeToFile:filePath atomically:YES]; 
} 

ただし、5.3MBファイルのダウンロードが完了すると、保存されているファイルのサイズが確認されます.1KBであるため、再生されません。私が望むように全体の代わりに小さなスライスを保存するだけですか?私は何をする必要がありますか?

+0

あなたは、このMP4を再生するために何を使用していますか?万が一「AVPlayer」ですか? – raistlin

+0

@FilipChwastowski私が知る限り、私はAVPlayerに基づいている 'MPMoviePlayerController'を使用しています。 – Jacksonkr

答えて

2

受信したデータを連結する必要があります。 NSMutableDataオブジェクトを見てください。データが完成したら、connectionDidFinishLoadingデリゲートメソッドでロジックを進めます。

receivedDataがダウンロードを開始する前に初期化するプロパティである、この例を使用します。

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
    [receivedData appendData:data]; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"BuckBunny.mp4"]; 

    [receivedData writeToFile:filePath atomically:YES]; 
    [connection release]; 
} 
2

上記の答えは、ダウンロード中にビデオ全体をメモリに保存します。これはおそらく小さな動画では問題ありませんが、これは大きな動画では使用できません。あなたは、このようなローカルドライブ上のファイルにデータを追加することができます

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
    NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:self.path]; 
    [handle seekToEndOfFile]; 
    [handle writeData:data]; 
} 
関連する問題