【问题标题】:don't show movie in document folder in xcode不要在 xcode 的文件夹中显示电影
【发布时间】:2013-05-09 13:32:17
【问题描述】:

我创建了一个在文件夹中显示文件的应用程序。 我有一个文件 .mov 格式,我想从文件夹中显示它,但是当运行应用程序并单击播放按钮时,不显示这部电影,我看到了这张图片 :

这是我的代码:(请指导我并告诉我我的错误)

- (IBAction)Play:(id)sender
{
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES)objectAtIndex:0];
    NSString *file = [documentPath stringByAppendingPathComponent:@"xcode4-outlet.mov"];
    NSURL *url = [NSURL fileURLWithPath:file];
    _moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:url];
    [self.view addSubview:_moviePlayer.view];
    _moviePlayer.fullscreen = YES;
    _moviePlayer.shouldAutoplay = YES;
    _moviePlayer.allowsAirPlay = YES;
    [_moviePlayer play];
}

编译器也帮我按摩:

movie player[9489:c07] [MPAVController] Autoplay: Likely to keep up or full buffer: 0
movie player[9489:c07] [MPAVController] Autoplay: Skipping autoplay, not enough buffered to keep up.
movie player[9489:c07] [MPAVController] Autoplay: Enabling autoplay
movie player[9489:c07] [MPCloudAssetDownloadController] Prioritization requested for media item ID: 0
movie player[9489:c07] [MPAVController] Autoplay: Enabling autoplay

【问题讨论】:

  • NSString *movPath = [[NSBundle mainBundle] pathForResource:@"xcode4-outlet" ofType:@"mov"]; 或检查电影退出与否[[NSFileManager defaultManager] fileExistsAtPath:file]
  • 我的朋友我想从文件夹中显示这部电影!!!

标签: ios objective-c mpavcontroller


【解决方案1】:

在添加电影播放器​​之前需要检查文件是否存在,也可以添加默认文件

-(IBAction)Play:(id)sender
{

    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES)objectAtIndex:0];
    NSString *file = [documentPath stringByAppendingPathComponent:@"xcode4-outlet.mov"];
    NSURL *url = [NSURL fileURLWithPath:file];

    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:file];
    if(fileExists)
    {
        _moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:url];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePreloadDidFinish:) name:MPMoviePlayerLoadStateDidChangeNotification
                                                   object:_moviePlayer];
        _moviePlayer.shouldAutoplay=NO;
        [_moviePlayer prepareToPlay];
    }

}

在电影完全加载后添加您的电影

-(void)moviePreloadDidFinish:(NSNotification*)notification
{

   _moviePlayer.controlStyle=MPMovieControlStyleDefault;
   [self.view addSubview:_moviePlayer.view];
   [_moviePlayer play];
   [_moviePlayer setFullscreen:YES animated:YES];

}

【讨论】:

  • 我在文件夹中的朋友是“xcode4-outlet.mov”但是 BOOL fileExists 让我没有价值!!!为什么?
  • 它的意思是你的文档目录中没有这样的文件名xcode4-outlet.mov。从查找器中检查此类文件是否存在
  • 我 nslog 文件并得到这个地址!!! :用户/fred/库/应用程序支持/iPhone 模拟器/6.0/Applications/21F0D83A-6C1A-47DC-916F-E37769E2973D/库/文档.......
  • 这不是文档目录的路径,它是库的路径,上面的路径是检查文档目录。
【解决方案2】:

您可能没有提供有效的 URL 路径。看起来您试图将资源附加到文档路径,但该资源实际上可能并不存在。您必须先将文件写入该路径。您应该始终仔细检查您的路径:

if ([[NSFileManager defaultManager] fileExistsAtPath:yourPath])
{
//path exists
}

否则,如果您的应用中有直接资源,则只需使用直接资源初始化电影播放器​​即可。

【讨论】:

  • 我的朋友在你写的顶级代码中“yourPath”就是这个 filePath = [documentsDirectoryPath stringByAppendingPathComponent:@"sample.mp4"]; ??????
  • 是的,“yourPath”是指您传递给 URL 的实际路径。所以是的,你的评论是正确的。它返回是还是否?
  • 这意味着您的电影文件不在该路径中。手动进入路径检查
  • 因为您必须事先将文件写入文档文件夹。为什么要从文档目录中使用它?除非它是用户选择的视频或其他东西,否则它并不是必需的......您可以直接使用资源初始化
【解决方案3】:

您的代码看起来不错。检查文件名是否正确..也试一试

-(void)playMovie
{
   NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
   NSString *filePath = [documentsDirectoryPath stringByAppendingPathComponent:@"xcode4-outlet.mov"];


    NSURL    *fileURL    =   [NSURL fileURLWithPath:filePath];
    MPMoviePlayerController *moviePlayerController = [[[MPMoviePlayerController alloc] initWithContentURL:fileURL]retain];
   [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(moviePlaybackComplete:)
                                             name:MPMoviePlayerPlaybackDidFinishNotification
                                           object:moviePlayerController];


   [self.view addSubview:moviePlayerController.view];

    moviePlayerController.fullscreen = YES;
   [moviePlayerController play];
  } 

  - (void)moviePlaybackComplete:(NSNotification *)notification
  {
   // playback completed 
  }

【讨论】:

  • 我的朋友moviePlaybackComplete:它是什么?
  • 当您的电影播放完毕时调用的通知方法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-17
  • 2015-02-26
  • 1970-01-01
  • 2019-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多