【问题标题】:Playing a video file saved in the app's documents directory播放保存在应用程序文档目录中的视频文件
【发布时间】:2012-12-23 11:26:40
【问题描述】:

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

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 传递给电影播放器​​。

【问题讨论】:

  • 如果可以,请使用 Web 视图显示文件。这更容易
  • 您在哪里将movieURL 传递给player
  • 你说得对,我没有将 URL 传递给播放器。我已经编辑了代码以包含它并且它有效。诚挚的歉意。

标签: iphone ios objective-c ios4 mpmovieplayercontroller


【解决方案1】:

您可以像这样播放文档目录中的视频:-

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

注意

请注意不要忘记包含请求的框架或连接代理

【讨论】:

    【解决方案2】:

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

    应该是

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

    您也在声明之前编写player 语句。

    【讨论】:

      猜你喜欢
      • 2016-07-27
      • 1970-01-01
      • 2023-02-13
      • 1970-01-01
      • 1970-01-01
      • 2016-06-24
      • 1970-01-01
      • 1970-01-01
      • 2016-10-28
      相关资源
      最近更新 更多