我正在计划使用此代码来使随机声音播放 Action ,而不是完全确定如何命名声音文件。谢谢你的帮助。
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];
最佳答案
像这样命名它们:
Sound01.mp3
声音02.mp3
...
声音24.mp3
关于iphone - 使用Xcode播放随机声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10038446/