Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: How to save a video in your directory and play it afterwards?

I am working in an iOS project. I want may application to download a video from the internet programmatically. Then I want to play it. I know how can I play a local video from the Resources, but my question is how could I download it , and the find it to be played. I am using MPMoviePlayerController to run the video.

Thanking in advance

like image 609
Ahd Radwan Avatar asked Dec 25 '22 19:12

Ahd Radwan


1 Answers

I found the answer

here I saved the video

    NSString *stringURL = @"http://videoURL";
NSURL  *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
NSString  *documentsDirectory ;
if ( urlData )
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   documentsDirectory = [paths objectAtIndex:0];

    NSString  *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"videoName.mp4"];
    [urlData writeToFile:filePath atomically:YES];

}

and this code is for playing the video form its directory

    NSString  *filepath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"videoName.mp4"];

//video URL
NSURL    *fileURL    =   [NSURL fileURLWithPath:filepath];
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[moviePlayerController play];
like image 200
Ahd Radwan Avatar answered Dec 28 '22 09:12

Ahd Radwan