Sam*_*Sam 5 iphone download nsurlconnection
我一直在为这个代码愚弄代码,如果有人能提供从服务器http://www.archive.org/download/june_high/june_high_512kb.mp4下载此文件的代码示例,我将非常感激.,(顺便说一下,它实际上不是这个文件,它只是一个试图帮助我的人的完美例子),然后从文档目录中播放它.我知道这对我来说似乎很懒,但我尝试了很多不同的NSURLConnection变种,这让我发疯了.此外,如果我确实设法下载了视频文件,我会认为这个代码会成功播放它是正确的:
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];
Run Code Online (Sandbox Code Playgroud)
如果上面的代码可以在文档目录中播放视频文件,那么我想我唯一需要知道的是,如何从服务器下载视频文件.这似乎是我的主要问题.任何帮助是极大的赞赏.
Pau*_*nch 18
您的代码将用于播放电影文件.
最简单的下载方式是同步:
NSData *data = [NSData dataWithContentsOfURL:movieUrl];
[data writeToURL:movieUrl atomically:YES];
Run Code Online (Sandbox Code Playgroud)
但是异步下载更好(对于应用程序响应等):
NSURLRequest *theRequest = [NSURLRequest requestWithURL:movieUrl cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
receivedData = [[NSMutableData alloc] initWithLength:0];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES];
Run Code Online (Sandbox Code Playgroud)
这需要实现非正式的NSURLConnection协议:
- (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];
}
Run Code Online (Sandbox Code Playgroud)
然后在movieReceived方法中保存(并播放)电影文件.
| 归档时间: |
|
| 查看次数: |
12001 次 |
| 最近记录: |