iphone - 使用Xcode播放随机声音

标签 iphone objective-c ios xcode audio

我正在计划使用此代码来使随机声音播放 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/

相关文章:

iphone - 通用 iPhone 和 iPad 应用程序的build设置是什么

ios - 如何按字母顺序将 NSArray 拆分为 UITableView 部分

ios - 将 UITableView 滚动到顶部无法正确滚动

iOS 在 UITableViewCell 上获取 subview 的引用

iphone - Interface Builder 文件中的未知类 ZBarReaderView

iphone - 每次我滑动到下一页时,内存都会增加很多。仪器泄漏报告和静态分析仪正常

ios - 在代码中混合 Storyboard和约束

iphone - MFMessageComposeViewController 未调用其委托(delegate)方法

ios - UIWebView 不显示 HTML

ios - Storyboard中的本地化和面向用户的文本