我想在我的应用中播放两首以上的歌曲。我怎么能用AVAudioPlayer做到这一点?
答案 0 :(得分:1)
我打算填写答案,但谷歌提供了一个非常好的答案:
页面中的第一段代码将按顺序调用所有声音:
- (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 完成比赛,然后你开始下一场比赛等等。