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.
Anybody any Idea how could I download the video from a url?
Thanx,
Gezim
Try this one:
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);
Create a NSURLRequest
, call [NSURLConnection connectionWithRequest:delegate:]
and implement the NSURLConnection delegate methods to receive the data as it is downloaded.