Playing MPMoviePlayerController Synchronously with AVAudioPlayer

為{幸葍}努か 提交于 2019-12-12 11:48:20

问题


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.



来源:https://stackoverflow.com/questions/9138441/playing-mpmovieplayercontroller-synchronously-with-avaudioplayer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!