用Xcode播放随机声音

时间:2012-04-06 02:25:59

标签: iphone objective-c ios xcode audio

我正在计划使用此代码使随机声音播放动作,而不是完全确定如何命名我的声音文件。谢谢你的帮助。

int randomNumber = arc4random() % 24 + 1;

NSURL *soundURL = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"Sound%02d", randomNumber] ofType:@"mp3"]];

AVAudioPlayer * soundPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:nil];

[soundPlayer prepareToPlay];
[soundPlayer play];

1 个答案:

答案 0 :(得分:3)

将它们命名为:

Sound01.mp3
Sound02.mp3
...
Sound24.mp3