为什么MPMoviePlayerController总是显示空黑屏?

时间:2014-04-14 04:16:45

标签: video ios7 mpmovieplayercontroller

初始视频播放器代码:

NSURL * url  =[NSURL URLWithString: @"http://www.ebookfrenzy.com/ios_book/movie/movie.mov"];
_player = [[MPMoviePlayerController alloc] initWithContentURL: url ];
[_player.view setFrame: self.view.bounds];
[self.view addSubview:_player.view];
_player.scalingMode = MPMovieScalingModeAspectFit;
      //    [_player prepareToPlay];
[_player play];

但是当我在ipad iOS 7中测试时,它总是显示黑屏,我是如何修复的?

2 个答案:

答案 0 :(得分:0)

将此代码放入您的项目中。添加按钮并将playMovie方法附加到它。 并在此处创建您的MPMoviPlayer对象

#import <MediaPlayer/MediaPlayer.h>
@interface ViewController ()
{
    MPMoviePlayerController *moviePlayer;
}

- (IBAction)playMovie:(id)sender {
    NSURL *url=[NSURL URLWithString:@"http://www.ebookfrenzy.com/ios_book/movie/movie.mov"];

    moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
    [moviePlayer setControlStyle:MPMovieControlStyleDefault];
    moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
    CGRect frame;
    if(self.interfaceOrientation ==UIInterfaceOrientationPortrait){
        frame = CGRectMake(20, 69, 280, 170);
    }
    else if(self.interfaceOrientation ==UIInterfaceOrientationLandscapeLeft || self.interfaceOrientation ==UIInterfaceOrientationLandscapeRight) {
        frame = CGRectMake(20, 61, 210, 170);
    }
    [moviePlayer.view setFrame:frame];  // player's frame must match parent's
    [self.view addSubview: moviePlayer.view];
    [self.view bringSubviewToFront:moviePlayer.view];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                     selector:@selector(moviePlayBackDidFinish:)
                                         name:MPMoviePlayerPlaybackDidFinishNotification
                                       object:moviePlayer];

    [moviePlayer prepareToPlay];
    moviePlayer.shouldAutoplay = YES;
}

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

    moviePlayer = [notification object];

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];

    if ([moviePlayer respondsToSelector:@selector(setFullscreen:animated:)])
    {
        [moviePlayer.view removeFromSuperview];
    }
}

这段代码可以帮助你。

答案 1 :(得分:0)

添加 的迅速

moviePlayer.prepareToPlay()

oc

[moviePlayer prepareToPlay];