ios - 不要在 xcode 的文档文件夹中显示电影

标签 ios objective-c mpavcontroller

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

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

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

最佳答案

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

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

}

关于ios - 不要在 xcode 的文档文件夹中显示电影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16557241/

相关文章:

objective-c - 用 cocoa 绘制线分隔符

ios - 在 CGContext 中绘制透明 PNG 显示为黑色

objective-c - 获取 pdf 中每个字形的边界框 - iOS

ios - 如何强制在 UIWebView 中播放的视频在 iPad 上全屏显示?

ios - 如何使用 SwiftUI 将图像和标签添加到选项卡栏项目

ios - AVAudioPlayer 即时更改声音 URL

ios - 当 ionic 2 页面完全加载时运行函数

ios - Xcode/Objective C - 动态导入文件(通过文件夹、正则表达式等)