播放保存在应用程序文档目录中的视频文件 [英] Playing a video file saved in the app's documents directory

查看:91
本文介绍了播放保存在应用程序文档目录中的视频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视频文件保存在我的应用程序的文档文件夹的本地目录中。我想在用户点击我创建的嵌入式表格视图中的项目时播放该文件。我播放视频的代码如下:

I have a video file saved in my app's local directory in the documents folder. I want to playback the file when the user clicks on the item in an embedded table view I created. My code for playing back the video is as follows:

NSString* documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* path = [documentPath stringByAppendingPathComponent:@"DemoRecording.mp4"];
NSURL* movieURL = [NSURL fileURLWithPath: path];
[player.view setFrame: self.view.bounds];
[self.view addSubView: player.view];
[player play];
MPMoviePlayerViewController* moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL: movieURL];
[self presentMoviePlayerViewControllerAnimated: moviePlayer];

视频无法播放。路径正确,文件存在。

我知道这是一个重复的问题。我已经提到了这些链接:

使用Cocoa-Touch从文档目录播放下载的视频

MPMoviePlayer加载和播放保存在应用文档中的电影

但是找不到确定的答案。

请帮我解决这个问题。我正在使用iOS 5.1.1,如果这改变了什么。

The video doesn't play back. The path is correct and the file exists there.
I know this is a repetitive question. I have referred to these links:
Playing a Downloaded Video from the Documents Directory with Cocoa-Touch
MPMoviePlayer load and play movie saved in app documents
But couldn't find a definitive answer.
Please help me out on this one. I am using iOS 5.1.1, if that changes anything.

编辑:

它确实有效。我忘记将URL传递给电影播放器​​了。

It does work. I had forgotten to pass the URL to the movie player.

推荐答案

你可以像这样从文档目录播放视频: -

you can play Video from Document Directory like this way:-

-(IBAction)playVideo
{
    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;

    for ( NSString *apath in filePathsArray )
    {
        fullpath = [documentsDirectory stringByAppendingPathComponent:apath];       
        vedioURL =[NSURL fileURLWithPath:fullpath];
    }
    NSLog(@"vurl %@",vedioURL);
    MPMoviePlayerViewController *videoPlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:vedioURL];
    [self presentMoviePlayerViewControllerAnimated:videoPlayerView];
    [videoPlayerView.moviePlayer play];     
}

注意


请注意,不要忘记包含请求的Framework或连接Delegate

Please note that don't Forget to include requested Framework or connect Delegate

这篇关于播放保存在应用程序文档目录中的视频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆