如何在ipad应用程序中从文件夹中播放视频文件

时间:2013-04-17 08:37:08

标签: iphone ipad video mpmovieplayercontroller document

我正在录制视频并将其保存到文档文件夹,我想稍后使用视频路径再次播放它但是它没有播放我正在使用以下代码获取文件夹中的文件路径然后播放

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



      NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
        [dateFormat setDateFormat:@"dd-MM-yyyy||HH:mm:SS"];
      NSDate *now = [[[NSDate alloc] init] autorelease];
      NSDate* theDate = [dateFormat stringFromDate:now];
      NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"Default Album"];
     if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
            [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:nil];
     NSString *videopath= [[[NSString alloc] initWithString:[NSString stringWithFormat:@"%@/%@.mov",documentsDirectory,theDate]] autorelease];
     NSLog(@"Vide Path %@",videopath);
     NSString *path = [[NSBundle mainBundle] pathForResource:videopath ofType:@"mov" inDirectory:nil];
     NSURL *movieURL = [NSURL fileURLWithPath:path];
     player= [[ MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
     [self presentMoviePlayerViewControllerAnimated:player];
你可以帮我谢谢。

2 个答案:

答案 0 :(得分:1)

而不是这个

NSString *videopath = [[[NSString alloc] initWithString:[NSString stringWithFormat:@"%@/%@.mov",documentsDirectory,theDate]] autorelease];

您可以在没有.mov扩展名的情况下尝试。因为你会在这一行告诉我们。

NSString *path = [[NSBundle mainBundle] pathForResource:videopath ofType:@"mov" inDirectory:nil];

注意:在此行中

player = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];

如果玩家保留了属性,则会导致内存泄漏..

答案 1 :(得分:0)

首先,您需要在项目中添加mediaplayer框架,然后添加import语句

#import <MediaPlayer/MediaPlayer.h>

然后按照以下代码

        MPMoviePlayerViewController *playerVC = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:aVideoUrl]];



        // Register this class as an observer instead
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(movieFinishedCallback:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:playerVC.moviePlayer];


        // Set the modal transition style of your choice
        //playerVC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

        [self presentMoviePlayerViewControllerAnimated:playerVC];
        [playerVC.moviePlayer prepareToPlay];
        [playerVC.moviePlayer play];
        [playerVC.moviePlayer setControlStyle:MPMovieControlStyleFullscreen];



- (void)movieFinishedCallback:(NSNotification*)aNotification
{
    // Obtain the reason why the movie playback finished
    NSNumber *finishReason = [[aNotification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];

    // Dismiss the view controller ONLY when the reason is not "playback ended"
    if ([finishReason intValue] != MPMovieFinishReasonPlaybackEnded)
    {
        MPMoviePlayerController *moviePlayer = [aNotification object];

        // Remove this class from the observers
        [[NSNotificationCenter defaultCenter] removeObserver:self
                                                        name:MPMoviePlayerPlaybackDidFinishNotification
                                                      object:moviePlayer];

        // Dismiss the view controller
        [self dismissModalViewControllerAnimated:YES];
    }
}