iphone – 与AVAudioPlayer同步播放MPMoviePlayerController

我正在尝试在游戏介绍中添加一部电影.这部电影没有声音/音乐.但是,我确实在后台播放音乐.音乐有点具体,因为音乐的介绍只要电影然后节奏开始更快一点.

我在一些设备上遇到的问题,如iPod 4G,过早播放音乐,然后基本上打破了电影序列.这在我的iPhone 4S中不会发生.

我试过寻找电影开始播放时的某种通知,然后开始播放音乐.我没有成功.

这是我到目前为止所得到的:

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

在摘要中,在iPhone 4S上它可以完美播放,但在iPod 4G上,音乐在电影开始前大约2秒钟开始播放,最终导致音频关闭.我该怎么做才能确保同步播放电影和音乐?

我不能做的事,除非另有说服力:

>我无法将音乐放在视频上,因为音乐需要循环播放几个视图.
>我无法将电影创建为UIImageView动画.

最佳答案 这是我做过的事情,以防有人碰到这个.

我为:MPMoviePlayerLoadStateDidChangeNotification创建了通知

当调用选择器返回MPMovieLoadStatePlaythroughOK的通知时,我播放了电影和音轨.

希望这可以帮助.

点赞