在查看Iphone sdk之前下载视频

时间:2010-02-11 17:25:31

标签: objective-c iphone mpmovieplayercontroller

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

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

感谢名单, Gezim

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:1)

创建NSURLRequest,调用[NSURLConnection connectionWithRequest:delegate:]并实施NSURLConnection委托方法,以便在下载数据时接收数据。