2012-04-26 173 views
0

我必須背對背播放兩個音頻文件。 我有一個通用文件,它說: 「我的名字是」在MPMoviePlayerController中播放音頻

然後跟隨另一個音頻文件,其中有另一個音頻字符串。

例如:說a.mp3有 「我的名字是」 b.mp3有 「約翰」

但是,當我設置的MPMoviePlayerController的的contentURL。它跳過第一個音頻並播放第二個音頻。

這裏是我的代碼:

- (void) playPromptAudio 
{[appDelegate.globalPlayer setContentURL:[NSURL 
      URLWithString:@"http://localhost/a.mp3"]]; 

    [appDelegate.globalPlayer prepareToPlay]; 
    [appDelegate.globalPlayer play]; 

    while (appDelegate.globalPlayer.playbackState == MPMoviePlaybackStatePlaying) { 
     // dirty hack, do gracefully 
     // do nothing 
    } 

    [appDelegate.globalPlayer setContentURL:[NSURL URLWithString:@"http://localhost/b.mp3"]]; 
     [appDelegate.globalPlayer prepareToPlay]; 
    [appDelegate.globalPlayer play]; 
} 

但是當我運行的代碼,唯一的「約翰」被播放。

調試完代碼後,我看到音頻在執行完整個函數後開始播放,因此很顯然「約翰」是從最後設置開始播放的。但是有沒有辦法阻止這種行爲?

回答

0

試試這個鏈接的MPMoviePlayerController播放

下面的代碼播放MpMoviePlayer與nodification:

 NSURL *url = [NSURL fileURLWithPath:filePath isDirectory:NO]; 
     self.moviePlayer = [[[MPMoviePlayerController alloc] initWithContentURL:url] 
        autorelease]; 
     moviePlayer.shouldAutoplay = NO; 
     moviePlayer.view.frame = view_video.bounds; 
     moviePlayer.scalingMode= MPMovieScalingModeFill; 
     moviePlayer.controlStyle =MPMovieControlStyleNone; 
     [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil]; 
     [[NSNotificationCenter defaultCenter] addObserver:self 
               selector:@selector(doneButtonClick:) 
                name:MPMoviePlayerPlaybackDidFinishNotification 
                object:nil]; 
     [moviePlayer Play]; 

以下函數被調用的MPMoviePlayerController Playbackfinished:

 -(void)doneButtonClick:(NSNotification*)aNotification{ 
     NSLog(@"MPMoviePlayer is finished..!"); 
    } 

謝謝....!

+0

謝謝,但鏈接是爲MPMusicPlayerController – krammer 2012-04-26 13:00:36

+0

好吧,現在我更新答案...! – Dinesh 2012-04-26 13:04:34

+0

我更新的答案試試這個..! – Dinesh 2012-04-26 13:11:42