2011-07-15 270 views
1

我正在構建一個遊戲應用程序,我需要播放動畫視頻及其相應的聲音(根據情況可能會有所不同)。我已經準備好所有的視頻和聲音文件,但我無法同時播放它們。我正在使用MPMoviePlayer用於視頻和AVFoundation框架來播放音頻。無論如何都可以同時運行這兩者嗎?iPhone:同時播放音頻和視頻

對於視頻:

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

感謝。

+0

取決於您如何使用AVFoundation。想發佈一些代碼? –

+0

請參閱編輯的問題。謝謝! – applefreak

回答

1

我個人會去AVPlayer路線。我知道你可能想要使用MPMoviePlayerViewController和AVAudioPlayer,但是如果你只是多花一點時間與AVPlayer組合使用AVMutableComposition,就可以做所有你想做的事以及更多。這是非常值得的投資。

總之,您將設置AVMutableComposition(也被視爲「資產」),並帶有軌道。這些曲目對應於音頻和視頻曲目。音軌可以從其他文件的全部或部分中拉出,或者您也可以根據需要插入空的音頻/視頻軌道部分。您將在視頻軌道上放置視頻,並在單獨的音軌上放置任意數量的音頻文件。然後使用AVMutableComposition創建一個AVPlayer對象並調用「播放」消息。它非常精確地爲你做了所有的同步。

This post實際上有一些很好的示例代碼,但我不確定他是否能夠正常工作。

+0

非常感謝。我認爲這是一個好主意! – applefreak

0

您可以嘗試使用AVMutableVideoComposition。試試看this thing,如果你需要任何幫助,請告訴我。我很樂意幫忙。