在查看Iphone SDK之前先下载视频 [英] Download video before view Iphone sdk

查看:60
本文介绍了在查看Iphone SDK之前先下载视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am developing an application that requires to play some video. However I do not want to pack the videos with application. Instead I would like to download videos in the NSDocumentDirectory and then play from there using MPMoviePlayerController.

任何人,我怎么能从URL下载视频?

Anybody any Idea how could I download the video from a url?

感谢, 吉济姆

推荐答案

尝试以下方法:

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);

这篇关于在查看Iphone SDK之前先下载视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆