Search code examples
videoios7mpmovieplayercontroller

Why MPMoviePlayerController alway show empty black screen?


Code for init video player:

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

But it always show black screen when I test in ipad iOS 7, how I fix it?


Solution

  • Put this code in your project. add button and attach the playMovie method to it. and create your MPMoviPlayer object here

    #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];
        }
    }
    

    may this code help you.