1

我正在开发一个需要播放一些视频的应用程序。但是我不想用应用程序打包视频。相反,我想在 NSDocumentDirectory 中下载视频,然后使用 MPMoviePlayerController 从那里播放。

任何人都知道如何从网址下载视频?

谢谢,Gezim

4

2 回答 2

4

试试这个:

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setHTTPMethod:@"GET"];
NSError *error;
NSURLResponse *response;

NSString *documentFolderPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *videosFolderPath = [documentFolderPath stringByAppendingPathComponent:@"videos"]; 

//Check if the videos folder already exists, if not, create it!!!
BOOL isDir;
if (([fileManager fileExistsAtPath:videosFolderPath isDirectory:&isDir] && isDir) == FALSE) {
    [[NSFileManager defaultManager] createDirectoryAtPath:videosFolderPath attributes:nil];
}


NSData *urlData;
NSString *downloadPath = @"http://foo.com/videos/bar.mpeg";
[request setURL:[NSURL URLWithString:downloadPath]];
urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *filePath = [videosFolderPath stringByAppendingPathComponent:@"bar.mpeg"];
BOOL written = [urlData writeToFile:filePath atomically:NO];
if (written)
    NSLog(@"Saved to file: %@", filePath);
于 2010-02-11T18:20:10.053 回答
1

创建一个NSURLRequest,调用[NSURLConnection connectionWithRequest:delegate:]并实现 NSURLConnection 委托方法来接收下载的数据。

于 2010-02-11T17:36:51.637 回答