iphone - 与 AVAudioPlayer 同步播放 MPMoviePlayerController

标签 iphone objective-c ios notifications 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 秒左右开始播放,这最终会导致介绍中断。如何确保电影和音乐同步播放?

我不能做的事情,除非另有确信:

  1. 我无法将音乐放在视频中,因为音乐会循环显示几个 View 。
  2. 我无法将电影制作成 UIImageView 动画。

最佳答案

如果有人遇到这个问题,我会采取以下措施。

我为 MPMoviePlayerLoadStateDidChangeNotification 创建了通知

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

希望这对您有所帮助。

关于iphone - 与 AVAudioPlayer 同步播放 MPMoviePlayerController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9138441/

相关文章:

iphone - 检查应用程序是否从非事件状态恢复到第一次启动?

ios - 绘制/渲染网格 iOS 的最佳方式 - 生命游戏

ios - CGRectOffset 后 AutoLayout 不工作

ios - 如何在 Objective-C 的不同 View 中共享本地变量

ios - 如何获取存储在sqlite数据库中的浮点值?

ios - 我正在使用 NavigationLink 在 View 之间导航,但它在 SwiftUI 中的单个 View 中不起作用

c++ - 为 iPhone 应用程序解析 RSS/Atom 提要的最佳方法是什么?

iphone - MapKit 用户位置(蓝点和圆圈)将不会显示

objective-c - 为什么 Xcode 3 显示多个编译器错误或每个实际发生的警告

ios - 为 Transformable 属性更改 Transformer 时是否需要进行核心数据迁移?