简体   繁体   中英

How to download video from url save it in directory and play using MPMovie player in ios

I tried till downloading but downloaded video is showing not supported plz help thanks for help.

Here is my code:

Downloading video from url code

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    NSLog(@"Downloading Started");
    NSString *urlToDownload = @"http://cdn-fms.rbs.com.br/vod/hls_sample1_manifest.m3u8";
    NSURL  *url = [NSURL URLWithString:urlToDownload];
    NSData *urlData = [NSData dataWithContentsOfURL:url];
    if ( urlData )
    {

        NSLog(@"urldata %@   ",urlData);
        NSArray   *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString  *documentsDirectory = [paths objectAtIndex:0];

        NSString  *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"thefifle2.mov"];

        //saving is done on main thread
        dispatch_async(dispatch_get_main_queue(), ^{
            [urlData writeToFile:filePath atomically:YES];
            NSLog(@"File Saved !%@",filePath);
             _url = [NSURL URLWithString:filePath];
            [self playv];

        });
    }

});

Play downloaded video using MPMoviepLayercontroller

    NSURL *vedioURL;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory  error:nil];
NSLog(@"files array %@", filePathsArray);

NSString *fullpath;

for (NSString *apath in filePathsArray )
{
     fullpath = [documentsDirectory stringByAppendingPathComponent:apath];
    vedioURL =[NSURL fileURLWithPath:fullpath];

}

 NSData *urlData1 = [NSData dataWithContentsOfURL:vedioURL];
 NSLog(@" url data1 %@  ",urlData1);
NSLog(@"vurl %@",vedioURL);
MPMoviePlayerViewController *videoPlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:vedioURL];
[self presentMoviePlayerViewControllerAnimated:videoPlayerView];
[videoPlayerView.moviePlayer play];

I tried with some mp4 Examples worked for me

//download the file in a seperate thread.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    NSLog(@"Downloading Started");
    NSString *urlToDownload = @"http://techslides.com/demos/sample-videos/small.mp4";
    NSURL  *url = [NSURL URLWithString:urlToDownload];
    NSData *urlData = [NSData dataWithContentsOfURL:url];
    if ( urlData )
    {

        NSLog(@"urldata %@   ",urlData);
        NSArray   *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString  *documentsDirectory = [paths objectAtIndex:0];

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

        //saving is done on main thread
        dispatch_async(dispatch_get_main_queue(), ^{
            [urlData writeToFile:filePath atomically:YES];
            NSLog(@"File Saved !%@",filePath);
             _url = [NSURL URLWithString:filePath];


        });
    }

});

You do realize that your URL doesn't actually point to a "video" file?

.m3u8 Files are not actual Asset files, rather, they are more akin to a playlist. They are used in HTTP Live Streaming and provide a location for "Segments" based on available bandwidth

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM