2

我创建了一个在文件夹中显示文件的应用程序。我有一个文件 .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
4

3 回答 3

5

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

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

}
于 2013-05-15T05:21:48.843 回答
4

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

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

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

于 2013-05-15T05:13:57.577 回答
1

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

-(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 
  }
于 2013-05-15T05:13:29.703 回答