我已经在这个代码上闲逛了很多年了,如果有人可以提供从服务器下载此文件的代码示例,我将非常感激http://www.archive.org/download/june_high/june_high_512kb.mp4 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];
如果上面的代码可以从文档目录播放视频文件,那么我想我唯一需要知道的是,如何从服务器下载视频文件。这似乎是我的主要问题。任何帮助是极大的赞赏。
您的代码将可以播放电影文件。
最简单的下载方式是同步:
NSData *data = [NSData dataWithContentsOfURL:movieUrl];
[data writeToURL:movieUrl atomically:YES];
但最好是异步下载(对于应用程序响应能力等):
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 协议:
- (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 方法中保存(并播放)电影文件。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)