Question

Je développe une application qui nécessite de jouer une vidéo. Cependant, je ne veux pas emballer les vidéos avec l'application. Au lieu de cela, je voudrais télécharger des vidéos dans le NSDocumentDirectory puis jouer à partir de là en utilisant MPMoviePlayerController.

Quelqu'un une idée comment pourrais-je télécharger la vidéo à partir d'une URL?

Thanx, Gezim

Était-ce utile?

La solution

Essayez celui-ci:

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

Autres conseils

Créer un NSURLRequest, appelez [NSURLConnection connectionWithRequest:delegate:] et mettre en œuvre les méthodes de délégué NSURLConnection pour recevoir les données qu'il est téléchargé.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top