iPhone SDK:如何将视频文件下载到文档目录然后播放? [英] iPhone SDK: How do you download video files to the Document Directory and then play them?
问题描述
我一直在为这个代码愚弄代码,如果有人能提供从服务器下载此文件的代码示例,我将非常感激 http://www.archive.org/download/june_high/june_high_512kb.mp4 ,(顺便说一下,它实际上不是这个文件,它只是一个任何试图帮助我的人的完美示例)然后从文档目录中播放它。我知道这对我来说似乎很懒,但我尝试了很多不同的NSURLConnection变种,这让我发疯了。
此外,如果我确实设法下载了视频文件,我会认为这个代码会成功播放它是正确的:
I've been fooling around with code for ages on this one, I would be very grateful if someone could provide a code sample that downloaded this file from a server http://www.archive.org/download/june_high/june_high_512kb.mp4, (By the way it's not actually this file, it's just a perfect example for anyone trying to help me) and then play it from the documents directory. I know it seems lazy of me to ask this but I have tried so many different variations of NSURLConnection that it's driving me crazy. Also, if I did manage to get the video file downloaded would I be correct in assuming this code would then successfully play it:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"june_high_512kb.mp4"];
NSURL *movieURL = [NSURL fileURLWithPath:path];
self.theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[_theMovie play];
如果以上代码可以在文档目录中播放视频文件,那么我猜是唯一的我需要知道的是,如何从服务器下载视频文件。这似乎是我的主要问题。非常感谢任何帮助。
If the above code would work in playing a video file from the document directory, then I guess the only thing I would need to know is, how to download a video file from a server. Which is what seems to be my major problem. Any help is greatly appreciated.
推荐答案
您的代码可以播放电影文件。
Your code will work to play a movie file.
最简单的下载方式是同步:
The simplest way to download is synchronously:
NSData *data = [NSData dataWithContentsOfURL:movieUrl];
[data writeToURL:movieUrl atomically:YES];
但异步下载更好(适用于应用响应等):
But it is better (for app responsiveness, etc) to download asynchronously:
NSURLRequest *theRequest = [NSURLRequest requestWithURL:movieUrl cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
receivedData = [[NSMutableData alloc] initWithLength:0];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES];
这需要实施非正式的NSURLConnection协议:
This requires implementing the informal NSURLConnection protocol:
- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[receivedData setLength:0];
}
- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[receivedData appendData:data];
}
- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[connection release];
}
- (NSCachedURLResponse *) connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse {
return nil;
}
- (void) connectionDidFinishLoading:(NSURLConnection *)connection {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[connection release];
[self movieReceived];
}
然后在movieReceived方法中保存(并播放)电影文件。
and then saving (and playing) the movie file in the movieReceived method.
这篇关于iPhone SDK:如何将视频文件下载到文档目录然后播放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!