0

I am trying to save video to document directory and play it again.I am doing app like youtube or like hotstar when i can save video in offline and play it in offline.

step 1:I am getting video url from the api and i am converting it to the data and saving it to the document directory using this code.

-(void)downloadVideoAndSave :(NSString*)videoUrl videoname:(NSString *)name
 {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    NSLog(@"Downloading Started");
    NSString *urlToDownload =videoUrl;
    NSURL  *url = [NSURL URLWithString:urlToDownload];
    NSData *urlData = [NSData dataWithContentsOfURL:url];
    if ( urlData )
    {
        NSArray       *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString  *documentsDirectory = [paths objectAtIndex:0];

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

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

});
}

2.now when user is in offline i am getting that videos from the document directory and I am trying to paly them using MPMoviePlayerViewController.but it is not workingout.I am using the below code to do that

-(void)playVideo:(UIButton*)sender {

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;
Store *info=[savedData objectAtIndex:sender.tag];

 NSString *videonamestr= info.videoname;
NSString *videofillname=[NSString stringWithFormat:@"/%@.mp4",videonamestr];
NSString  *filePath = [NSString stringWithFormat:@"%@/%@%@", documentsDirectory,videonamestr,@".mp4"];
 //fullpath = [documentsDirectory stringByAppendingPathComponent:videofillname];
// vedioURL =[NSURL fileURLWithPath:fullpath];
NSFileManager *fileManager = [NSFileManager defaultManager];

if ([fileManager fileExistsAtPath: fullpath]) //4
{
    NSLog(@"videoexists");
    //NSString *bundle = [[NSBundle mainBundle] pathForResource:filename ofType:@"plist"]; //5

    //[fileManager copyItemAtPath:bundle toPath:path error:&error]; //6
}
//    for ( NSString *apath in filePathsArray )
//    {
//        fullpath = [documentsDirectory stringByAppendingPathComponent:apath];
//        vedioURL =[NSURL fileURLWithPath:fullpath];
//    }
NSLog(@"vurl %@",filePath);

//    for (Store *items in savedData) {
//        [self downloadVideoAndSave:items.videopath];

//        NSLog(@"\n mobile data  %@\n",items.videopath);
//        
    UIStoryboard *story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    PlayerViewController *playerVC=[story instantiateViewControllerWithIdentifier:@"playerViewController"];
    playerVC.comingString = filePath;
    [self presentViewController:playerVC animated:NO completion:nil];

 // }


 }

3.In the the nextvc mpmoviewolayer

NSURL *fileUrl = [NSURL URLWithString:self.comingString];

self.moviePlayerViewController = [[MPMoviePlayerViewController alloc]initWithContentURL:fileUrl];

self.moviePlayerViewController.moviePlayer.fullscreen = NO;
[self.moviePlayerViewController.moviePlayer play];
self.moviePlayerViewController.moviePlayer.repeatMode = NO;
self.moviePlayerViewController.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;

[self.view addSubview:self.moviePlayerViewController.view];
[self presentViewController:self.moviePlayerViewController animated:NO completion:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(playFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];

please help me to solve this problem and where I am making mistake.Please dont down mark in that case please answer.Thanks for quick responce.

  • Shouldn't this line `if ([fileManager fileExistsAtPath: fullpath])` be `if ([fileManager fileExistsAtPath: filepath])`? Do you know the `mp4` actually exists – Flexicoder Nov 09 '17 at 17:06

1 Answers1

0

In your playerVC this line...

NSURL *fileUrl = [NSURL URLWithString:self.comingString];

should be

[NSURL fileURLWithPath:self.comingString];
Flexicoder
  • 8,251
  • 4
  • 42
  • 56
  • thank you flexcioder is there any other way of storing video where you can pause restart download and video progress with perencentage. – Satheeshkumar Naidu Nov 10 '17 at 03:35
  • @SatheeshkumarNaidu did this answer your question? If so please mark it as answered so that others searching can see. Your comment sounds like another question, which should be raised separately – Flexicoder Nov 10 '17 at 08:35