【问题标题】:Playing a Downloaded Video from the Documents Directory with Cocoa-Touch使用 Cocoa-Touch 播放从 Documents 目录下载的视频
【发布时间】:2012-07-17 22:43:57
【问题描述】:

我正在尝试让我的应用播放已下载到文档目录的视频文件。我知道文件正在下载,但我似乎无法播放文件,这是我的代码:

-(IBAction)play{

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"%@/piggy.m4v"];

NSURL *movieURL = [NSURL fileURLWithPath:path];


_player = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[self.view addSubview:_player.view];

_player.controlStyle = MPMovieControlStyleDefault;
_player.shouldAutoplay = YES;


[_player setFullscreen:YES animated:YES];


[_player play];

}

【问题讨论】:

  • 您在不需要 %@/ 的 NSString *path 定义中有错误。但是你还没有说如果你从控制台得到一个输出或错误是什么。
  • 你确定这个东西存在于文档目录而不是应用程序包中吗?我可以从捆绑包中加载视频。
  • 一句忠告:我的应用程序因将视频保存到 /Documents 而被拒绝,根据新的存储指南(底线:它使备份变慢/繁重)。 如果您的目标是 iOS 5.0.1 或更高版本,您可以将 /Documents 中的子目录标记为“跳过备份”。否则,您唯一的选择是保存到 /Caches(并设计一种在视频被删除时重新下载视频的方法)
  • 感谢您的建议,我会尝试设置一个子目录。

标签: ios cocoa-touch mpmovieplayercontroller nsdocumentdirectory


【解决方案1】:

此行将出现问题:NSString *path = [documentsDirectory stringByAppendingPathComponent:@"%@/piggy.m4v"];

将其更改为NSString *path = [documentsDirectory stringByAppendingPathComponent:@"piggy.m4v"];

【讨论】:

  • 我从控制台得到上述输出,我刚刚再次运行应用程序,也得到了这个错误 Error (null) setting player mode to 'AVAudioSessionModeDefault'
【解决方案2】:

这看起来像是某种错误,但您必须这样设置路径:

 NSString *vidPath = [[NSBundle mainBundle] pathForResource:@"promo" ofType:@"mp4"];
 NSURL *url = [NSURL fileURLWithPath:vidPath isDirectory:NO]; //THIS IS THE KEY TO GET THIS RUN :) 
 [introPlayer setContentURL:url];

【讨论】:

  • 非常感谢。你救了我
猜你喜欢
  • 1970-01-01
  • 2019-02-22
  • 2011-11-23
  • 1970-01-01
  • 1970-01-01
  • 2022-10-11
  • 1970-01-01
  • 1970-01-01
  • 2016-07-18
相关资源
最近更新 更多