iPhone:同时播放音频和视频
我正在构建一个游戏应用程序,我需要在其中播放动画视频及其相应的声音(根据情况可能会有所不同)。我已准备好所有视频和声音文件,但无法同时播放它们。我使用 MPMoviePlayer 播放视频,使用 AVFoundation 框架播放音频。有没有办法同时运行这两个?
对于视频:
MPMoviePlayerViewController *videoController = [[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:[cellDetails objectForKey:@"AVFile"]]] autorelease];
[self presentMoviePlayerViewControllerAnimated:videoController];
对于声音:
// Instantiates the AVAudioPlayer object, initializing it with the sound
AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: soundFileURL error: nil];
self.appSoundPlayer = newPlayer;
[newPlayer release];
[appSoundPlayer prepareToPlay];
appSoundPlayer.numberOfLoops = -1;
[appSoundPlayer setVolume: self.soundLevel];
[appSoundPlayer setDelegate: self];
[appSoundPlayer play];
谢谢。
I am building a game application where I need to play animation videos and their corresponding sounds(may be different depending on situation). I have all videos and sounds file are ready but I am not able to play them simultaneously. I am using MPMoviePlayer for video and AVFoundation framework to play audio. Is there anyway to run these both simultaneously?
For Video:
MPMoviePlayerViewController *videoController = [[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:[cellDetails objectForKey:@"AVFile"]]] autorelease];
[self presentMoviePlayerViewControllerAnimated:videoController];
For Sound:
// Instantiates the AVAudioPlayer object, initializing it with the sound
AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: soundFileURL error: nil];
self.appSoundPlayer = newPlayer;
[newPlayer release];
[appSoundPlayer prepareToPlay];
appSoundPlayer.numberOfLoops = -1;
[appSoundPlayer setVolume: self.soundLevel];
[appSoundPlayer setDelegate: self];
[appSoundPlayer play];
Thanks.
我个人会走 AVPlayer 路线。我知道您可能想要使用 MPMoviePlayerViewController 和 AVAudioPlayer,但如果您只需在带有 AVPlayer 的 AVMutableComposition 上投入更多时间,您就可以完成您正在尝试的所有操作,甚至更多。这是非常值得的投资。
简而言之,您将设置一个带有轨道的 AVMutableComposition(也被视为“资产”)。这些轨道对应于音频和视频轨道。可以从其他文件的全部或部分中提取轨道,也可以根据需要插入空的音频/视频轨道部分。您可以将视频放在视频轨道上,并将任意数量的音频文件放在单独的音轨上。然后,您使用 AVMutableComposition 创建一个 AVPlayer 对象并调用“播放”消息。它非常精确地为您完成所有同步。
这篇文章实际上有一些很好的示例代码但我不确定他是否能正常工作。
I personally would go the AVPlayer route. I know you probably want to go with MPMoviePlayerViewController and AVAudioPlayer, but if you just invest a little bit more time into AVMutableComposition with an AVPlayer, you can do everything you're trying and more. It's well worth the investment.
In short, you'll setup an AVMutableComposition (which is also considered an "asset"), with tracks. These tracks correspond to audio and video tracks. The tracks can be pulled from all or portions of other files or you can also insert empty audio/video track portions as you wish. You'll put a video on a video tracks and any number of audio files on separate audio tracks. Then you create an AVPlayer object using the AVMutableComposition and call the "play" message. It does all of the syncing for you very precisely.
This post actually has some good sample code but I'm not sure if he's got it working correctly yet.