MPMoviePlayerController:不播放视频

时间:2015-03-10 04:09:34

标签: ios xcode mpmovieplayercontroller alassetslibrary

我尝试使用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& #34;但是当我有一个类似"的文件:///var/mobile/Media/DCIM/101APPLE/IMG_1454.mp4"它没有用。

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

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

3 个答案:

答案 0 :(得分:2)

相同的代码对我有用,我这样使用它。由于获取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];
}];

答案 1 :(得分:1)

我猜您在

中使用NSUrl *url = [NSURL URLWithString:urlStr]; url
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];

但是像filePath那样:file:///var/mobile/Media/DCIM/101APPLE/IMG_1454.mp4 你必须使用:

NSUrl *url =  [NSURL fileURLWithPath:filePath];

请参阅NSUrl Document

答案 2 :(得分:0)

我对你的代码进行了一些更改,并将电影播放器​​代码放在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];

        }];

请检查它是否可以帮助你...