2012-03-20 79 views
0

我想在我的應用程序中播放兩首以上的歌曲。我該如何使用AVAudioPlayer做到這一點?如何使用AVAudioPlayer播放兩首以上的歌曲

+0

你想在同一時間或不同時間打他們?歌曲的格式是什麼? – 2012-03-20 05:50:34

+0

我想一個一個地播放它。當一首歌完成後,它會自動開始另一首歌。我將所有歌曲存儲在一個項目目錄中。 – 2012-03-20 05:53:54

回答

1

我要鍵入了答案,但谷歌提供了一個完美的答案:

http://www.iphonedevsdk.com/forum/iphone-sdk-development/27285-play-sequence-audio-files-avaudioplayer.html

的代碼頁的第一塊將只需要調用所有的聲音都在序列:

- (void)playSoundSequence { 

    // Make array containing audio file names 
    NSArray *theSoundArray = [NSArray arrayWithObjects:@"one",@"two",nil]; 

    int totalSoundsInQueue = [theSoundArray count]; 

    for (int i=0; i < totalSoundsInQueue; i) { 

     NSString *sound = [theSoundArray objectAtIndex:i]; 

       // Wait until the audio player is not playing anything 
     while(![audioPlayer isPlaying]){ 

      AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] 
               pathForResource:sound ofType:@"wav"]] error:NULL]; 
      self.audioPlayer = player; 
      [player release]; 
      [audioPlayer setVolume:1.0f]; 
      [audioPlayer play]; 
      //Increment for loop counter & move onto next iteration 
         i++;  
     } 
    } 

} 

但是,你可能最好使用AVAudioPlayerDelegate:

如果你不想在循環等待,你也可以讓你 控制器的AVAudioPlayerDelegate和實施

代碼:

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag 

這將被調用時的結束音頻文件到達,在 哪一點你會開始播放下一個音頻文件。 audioPlayerDidFinishPlaying隨後會再次被調用時一個 結束播放時,你開始後,下一個,等

+0

謝謝Nathan ....讓我試試..... – 2012-03-20 06:04:13

相關問題