繁体   English   中英

播放保存在应用程序文档目录中的视频文件

[英]Playing a video file saved in the app's documents directory

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

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,如果这改变了什么。

编辑:

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

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

-(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

你错过了/在DemoRecording.mp4之前

它应该是

NSString* path = [documentPath stringByAppendingPathComponent:@"/DemoRecording.mp4"];

你也在宣布player声明之前编写player声明。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM