2012-03-20 177 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

相关问题