2013-01-09 40 views
4

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

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

视频无法播放。路径是正确的,并且文件存在那里。
我知道这是一个重复的问题。我已经提到这些链接:
Playing a Downloaded Video from the Documents Directory with Cocoa-Touch
MPMoviePlayer load and play movie saved in app documents
但找不到确切答案。
请帮我解决这个问题。我正在使用iOS 5.1.1,如果这改变了任何东西。

编辑:

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

+0

如果可以,请使用web视图来显示该文件。这很容易 –

+1

你在哪里将'movieURL'传递给'player'? – Zen

+0

你是对的我没有把URL传给玩家。我编辑了代码来包含它,它的工作原理。真诚的歉意。 –

回答

19

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

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

注意

请注意,不要忘了包括要求框架或连接代理

+0

很好...它的工作正常,没有任何错误.. – svmrajesh

1

您的错误是您在DemoRecording.mp4之前错过了/

应该

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

而且您声明之前写player语句。

相关问题