如何从网址下载视频将其保存在目录中并使用ios中的MPMovie播放器播放

Kum*_*amy 1 objective-c ios

我试着下载但下载的视频显示不支持plz帮助感谢您的帮助.

这是我的代码:

从网址代码下载视频

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

        });
    }

});
Run Code Online (Sandbox Code Playgroud)

使用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];
Run Code Online (Sandbox Code Playgroud)

Kum*_*amy 5

我尝试了一些mp4实例为我工作

//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];


        });
    }

});
Run Code Online (Sandbox Code Playgroud)