4

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.
RoLYroLLs
  • 3,113
  • 4
  • 38
  • 57
  • Use the playback state notification the of MPMoviePlayerController to sync the start of the audio with the video. – Till Feb 04 '12 at 20:18
  • @Till - thanks, I tried this and still get the same issue. If I defer the music to play about 1 second after calling the `@selecter` for the notification, the iPod works great, but on the iPhone the songs lags behind. – RoLYroLLs Feb 07 '12 at 00:27
  • 1
    Then how about doing as I said, wait for the MPMoviePlayerController playback state "playThroughOK", pause the video at that point. Then wait for the AVAudioPlayer to start its playback (using KVO on currentTime) and then unpause the video playback. – Till Feb 12 '12 at 04:28
  • @Till - awesome that worked! Post it as an answer so I can give you credit and the points. Although the playThrough ok is a loadState, not a playBackState :) – RoLYroLLs Feb 12 '12 at 21:10

1 Answers1

2

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.

RoLYroLLs
  • 3,113
  • 4
  • 38
  • 57