1

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

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

你能帮帮我吗谢谢。

4

2 回答 2

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

如果播放器有保留属性,会导致内存泄漏..

于 2013-04-17T08:43:31.937 回答
0

首先您需要在项目中添加媒体播放器框架,然后添加导入语句

#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];
    }
}
于 2013-04-17T12:10:08.087 回答