Playing MPMoviePlayerController Synchronously with

2019-06-25 03:10发布

问题:

I'm trying to add a movie to the intro of a game. The movie has no sound/music. I do, however, have music looping in the background. The music is somewhat specific in that the intro of the music is as long as the movie and then the tempo kicks in a bit faster.

The problem I am having in that on some devices, like the iPod 4G, play the music too soon, which then basically breaks the movie sequence. This does not happen in my iPhone 4S.

I have tried looking for some sort of notification of when the movie has started playing, and then start the music. I have been unsuccessful.

Here's what I got so far:

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"SplashVideo" ofType:@"mp4"]]];

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

    [[player view] setFrame:CGRectMake(0, 0, 480, 320)];
    [player setControlStyle:MPMovieControlStyleNone];
    [[self view] addSubview:[player view]];

    // play movie
    [player play];

    //musicPlayer is defined in the header
    [self setMusicPlayer:[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"SplashMusic" ofType:@"m4a"]] error:nil]];
    [[self musicPlayer] setNumberOfLoops:loop];
    [[self musicPlayer] prepareToPlay];
    [[self musicPlayer] setDelegate:self];
}

- (void)moviePlayerPlaybackDidFinish:(NSNotification*) aNotification {
    DLog(@"**********");

    MPMoviePlayerController *player = [aNotification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player];

    [player stop];
    [[player view] removeFromSuperview];

    [player autorelease];
}

In Summary, on the iPhone 4S it plays flawlessly, but on the iPod 4G, the music starts about 2 seconds before the movie does, which ends up throwing the intro off. What can I do to make sure the movie and the music are played synchronously?

Things I cannot do, unless otherwise convinced:

  1. I cannot put the music on the video as the music is meant to loop through a few views.
  2. I cannot created the movie into a UIImageView animation.

回答1:

Here's what I did in case someone runs into this.

I created notification for: MPMoviePlayerLoadStateDidChangeNotification

When the selector was called returning the notification of MPMovieLoadStatePlaythroughOK I played the movie and the sound track.

Hope this helps.