简体   繁体   中英

How can i play video (By Name) from document directory ?

In my application i store my video in document directory with name is 'video2.MOV' and i want to play it.

Here problem is that i am not able to get this video (name is 'video2.MOV' ) from document directory.

My Code :

-(void)playVideo:(NSTimer *)theTimer
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"video2.MOV"];
    NSLog(@"filePath - %@",filePath);

    NSString *content = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];
    NSLog(@"contentPath - %@",content);

    MPMoviePlayerViewController *videoPlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:content]];
    [[CCDirector sharedDirector] presentMoviePlayerViewControllerAnimated:videoPlayerView];
    [videoPlayerView.moviePlayer play];
}

In console, each time i got contentPath is NULL , so may be i am not able to play video.

Here, What is my mistake. please give me suggestion on this issue.

Try this:

- (NSString*)GetDocumentFilePath:(NSString*)filename
{
    NSLog(@"%@",filename);
    // NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //1
    NSString *documentsDirectory = [paths objectAtIndex:0]; //2
    NSString* file = [[NSString alloc]initWithFormat:@"%@",filename];
    NSString* path = [documentsDirectory stringByAppendingPathComponent:file];
    NSFileManager *fileManager = [NSFileManager defaultManager];

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

        //[fileManager copyItemAtPath:bundle toPath:path error:&error]; //6
    }  

    return path;
}

and get file path like this:

 NSURL *URL = [NSURL fileURLWithPath:[self GetDoucmentPath:path]];


MPMoviePlayerController* theMovie=[[MPMoviePlayerController alloc] initWithContentURL:pptURL];
    theMovie.scalingMode=MPMovieScalingModeAspectFill;

    theMovie.view.frame = CGRectMake(60, 20, 700, 500);
    theMovie.view.center = self.view.center;

    //[self.view addSubview:theMovie.view];

    // Register for the playback finished notification. 

    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(onStop) 
                                                 name:MPMoviePlayerPlaybackDidFinishNotification 
                                               object:theMovie];

    // Movie playback is asynchronous, so this method returns immediately. 

    [self.view addSubview:theMovie.view];
    [theMovie play];

Hope it Helps!!

与其使用content filePath使用filePath来访问电影文件:

 MPMoviePlayerViewController *videoPlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:filePath]];

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