简体   繁体   English

如何播放文档目录中的视频(按名称)?

[英]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. 在我的应用程序中,我将视频存储在文档目录中,名称为'video2.MOV' ,我想播放它。

Here problem is that i am not able to get this video (name is 'video2.MOV' ) from document directory. 这里的问题是我无法从文档目录中获取此视频(名称为'video2.MOV'

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. 在控制台中,每次获得contentPath均为NULL ,因此可能无法播放视频。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过从 PhotoLibrary 获取视频来播放视频 - How can i play video by getting Video from PhotoLibrary 如何显示从文档目录到图像视图的图像? - How can i display images from document directory to image view? iPhone SDK:如何将视频文件下载到文档目录然后播放? - iPhone SDK: How do you download video files to the Document Directory and then play them? 我试图从“文档”目录访问视频,但出现错误“无此文件或目录” - I am trying to access the video from the Document directory and i am getting error “No such file or directory” 我有一个保存在 xcode 中的视频:如何播放? - I have a video saved in xcode: how can I play it? 如何获取保存在文档目录中的视频的缩略图 - How to get thumbnails of a video saved in document directory 如何将视频文件保存到文档目录中 - how to save video file into document directory 如何使其在android / iphone上播放视频? - how can i make it play video on android/iphone? 如何通过触摸tableviewcell中视频的视频缩略图上的按钮来播放视频? - How can I play video by touching the button on the video thumnail of the video in tableviewcell? 如何从其他iOS应用程序读取应用程序文档目录中的文档? - How can I read documents in my app's Document directory from other iOS apps?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM