Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MPMoviePlayerController causes flash of black at start of video

Tags:

ios4

The movie plays just fine but there is a quick black flash right before it plays. Is this a quirk resulting from setting the controlstyle to MPMovieControlStyleNone?

NSString *url = [[NSBundle mainBundle] pathForResource:@"00" ofType:@"mov"];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] 
    initWithContentURL:[NSURL fileURLWithPath:url]];

[[NSNotificationCenter defaultCenter]
    addObserver:self
    selector:@selector(movieFinishedCallback:)
    name:MPMoviePlayerPlaybackDidFinishNotification
    object:player];

//---play video in implicit size---
player.view.frame = CGRectMake(80, 64, 163, 246);
[self.view addSubview:player.view];

// Hide video controls
player.controlStyle =  MPMovieControlStyleNone;

//---play movie---
[player play];
like image 426
Marsman Avatar asked Nov 18 '10 14:11

Marsman


2 Answers

I just had this problem and fixed it by adding an observer to the default NSNotificationCenter to find out when the movie was completely ready to play, and THEN adding the view as a subview to my main view.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkMovieStatus:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];

...

if(moviePlayer.loadState & (MPMovieLoadStatePlayable | MPMovieLoadStatePlaythroughOK))
{
    [pageShown.view addSubview:moviePlayer.view];
    [moviePlayer play];
}
like image 115
Marty Avatar answered Oct 14 '22 13:10

Marty


In IOS 6 mpmoviewplayer added a new property :readyForDisplay

this is what I'm playing around with and so far so good:

  1. create mpmovieplayer ,add to stage, hide it.
  2. add notification for play state on the movieController
  3. wait for the displayState to Change and once its ready show the video controller:

    - (void)moviePlayerPlayState:(NSNotification *)noti {
    
    if (noti.object == self.movieController) {
    
        MPMoviePlaybackState reason = self.movieController.playbackState;
    
         if (reason==MPMoviePlaybackStatePlaying) {
    
                 [[NSNotificationCenter defaultCenter] removeObserver:self name: MPMoviePlayerPlaybackStateDidChangeNotification object:nil];
    
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
    
                while (self.movieController.view.hidden)
                {
                    NSLog(@"not ready");
                    if (self.movieController.readyForDisplay) {
    
                     dispatch_async(dispatch_get_main_queue(), ^(void) {
                         NSLog(@"show");
                         self.movieController.view.hidden=NO;
                     });
    
                    }
                    usleep(50);
                }
            });
         }
    
    }
    

    }

When the play state changes to MPMoviePlaybackStatePlaying we start checking for the readyDisplayState to change.

like image 36
yeahdixon Avatar answered Oct 14 '22 13:10

yeahdixon