2012-05-09 3 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 파일 다운로드가 끝나면 저장된 파일의 크기를 확인하고 결과적으로 재생되지 않습니다. 내가 원하는 것보다 작은 조각을 저장하는 것일까 요? 다르게해야 할 일은 무엇입니까?

+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]; 
} 
관련 문제