【问题标题】:MPMoviePlayerController: Not playing videoMPMoviePlayerController:不播放视频
【发布时间】:2015-03-10 04:09:33
【问题描述】:

我正在尝试使用 MPMoviePlayerController 播放本地保存的视频,但它不起作用。

这是我的来源:

self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.moviePlayer prepareToPlay];

CGRect frame = self.movieView.frame;
frame.origin = CGPointZero;

self.moviePlayer.view.frame = frame;

self.moviePlayer.allowsAirPlay         = NO;
self.moviePlayer.shouldAutoplay        = NO;
self.moviePlayer.movieSourceType       = MPMovieSourceTypeFile;
self.moviePlayer.scalingMode           = MPMovieScalingModeAspectFit;
self.moviePlayer.controlStyle          = MPMovieControlStyleEmbedded;
self.moviePlayer.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

[self.movieView addSubview:self.moviePlayer.view];

当我有一个类似“assets-library://asset/asset.MOV?id=2C7D89F6-2211-4920-A842-30D773B075D6&ext=MOV”的网址时,它确实工作,但是当我有像“file:///var/mobile/Media/DCIM/101APPLE/IMG_1454.mp4”这样的 URL 不起作用。

我正在使用下面的代码来获取用户的最新视频并在播放器中播放:

- (void)mostRecentVideo:(void (^)(NSURL *url))completion
{
PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];

PHFetchResult *fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeVideo options:fetchOptions];
PHAsset *lastAsset = [fetchResult lastObject];

PHVideoRequestOptions *options = [PHVideoRequestOptions new];
options.deliveryMode = PHVideoRequestOptionsDeliveryModeMediumQualityFormat;

[[PHImageManager defaultManager] requestAVAssetForVideo:lastAsset options:options resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
    if ([asset isKindOfClass:[AVURLAsset class]])
        completion(((AVURLAsset *)asset).URL);
}];
}

【问题讨论】:

  • 你是如何在 self.movi​​ePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url] 中得到 url 的;
  • mostRecentVideo函数是我弄到的,第一次用UIImagePickerController弄到
  • 不起作用是什么意思?什么行为?
  • @gabbler 基本上没有显示视频。它只是显示黑屏,或保留最后一个视频

标签: ios xcode mpmovieplayercontroller alassetslibrary


【解决方案1】:

同样的代码对我有用,我是这样使用的。由于获取 url 是异步操作,因此必须在主队列上更新 UI。只需将代码放入viewDidAppear 再试一次。

[self mostRecentVideo:^(NSURL *url){
    NSLog(@"did play!, url is %@",url);
    self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
    [self.moviePlayer prepareToPlay];

    CGRect frame = self.view.frame;
    frame.origin = CGPointZero;

    self.moviePlayer.view.frame = frame;

    self.moviePlayer.allowsAirPlay         = NO;
    self.moviePlayer.shouldAutoplay        = NO;
    self.moviePlayer.movieSourceType       = MPMovieSourceTypeFile;
    self.moviePlayer.scalingMode           = MPMovieScalingModeAspectFit;
    self.moviePlayer.controlStyle          = MPMovieControlStyleEmbedded;
    self.moviePlayer.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

    dispatch_async(dispatch_get_main_queue(), ^{
        [self.view addSubview:self.moviePlayer.view];
    });
    [self.moviePlayer play];
}];

【讨论】:

  • 我已经尝试了上面的代码,但它出现了一个空白屏幕!我能听到声音但没有看到视频!
  • @kb920,我不知道为什么。
【解决方案2】:

我猜你在url 中使用NSUrl *url = [NSURL URLWithString:urlStr];

self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];

但文件路径如:file:///var/mobile/Media/DCIM/101APPLE/IMG_1454.mp4 你必须使用:

NSUrl *url =  [NSURL fileURLWithPath:filePath];

NSUrl Document

【讨论】:

  • 我使用的 URL 是 '((AVURLAsset *)asset).URL',这就是行不通的。我应该把它转换成字符串还是什么?
  • 是 file:///var/mobile/Media/DCIM/101APPLE/IMG_1454.mp4 结果 (AVURLAsset *)asset).URL 吗?
  • 是的,它们都是 NSURL 的
【解决方案3】:

我对您的代码进行了一些更改,并将电影播放器​​代码放在 PHImageManager 块中,它对我有用。

      [[PHImageManager defaultManager] requestAVAssetForVideo:lastAsset options:options resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
            if ([asset isKindOfClass:[AVURLAsset class]])
                NSLog(@"%@",((AVURLAsset *)asset).URL);

            AVURLAsset *assetURL = [AVURLAsset assetWithURL:((AVURLAsset *)asset).URL];

            NSString *str= [NSString stringWithFormat:@"%@",assetURL.URL];

            NSURL *url = [NSURL URLWithString:str];

            // Initialize the MPMoviePlayerController object using url
            _videoPlayer =  [[MPMoviePlayerController alloc]
                             initWithContentURL:url];

            [_videoPlayer.view setFrame:CGRectMake(0, 0, 320, 568)];

            // Add a notification. (It will call a "moviePlayBackDidFinish" method when _videoPlayer finish or stops the plying video)
            [[NSNotificationCenter defaultCenter] addObserver:self
                                                     selector:@selector(moviePlayBackDidFinish:)
                                                         name:MPMoviePlayerPlaybackDidFinishNotification
                                                       object:_videoPlayer];

            // Set control style to default
            _videoPlayer.controlStyle = MPMovieControlStyleDefault;

            // Set shouldAutoplay to YES
            _videoPlayer.shouldAutoplay = YES;
            [_videoPlayer setMovieSourceType:MPMovieSourceTypeFile];

            // Add _videoPlayer's view as subview to current view.
            [self.view addSubview:_videoPlayer.view];

            // Set the screen to full.
            [_videoPlayer setFullscreen:YES animated:YES];

        }];

请检查它是否对您有帮助...

【讨论】:

  • 抱歉花了这么长时间才回复,但那个来源对我不起作用...播放器是否有错误记录?
猜你喜欢
  • 2012-11-29
  • 2013-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-11
  • 1970-01-01
相关资源
最近更新 更多