在iOS中创建一个mp3音频文件数组,并随机使用UIButton播放它们

时间:2013-02-24 08:42:13

标签: objective-c xcode arrays ios5

我正在尝试创建一个预测制作iOS 5 iPhone应用程序,它将播放一个可以由UIButton的touchUpInside事件触发的阵列中的短(1-15秒长)随机mp3音频剪辑。我想创建一个包含mp3文件的数组,然后创建一个方法,在调用时将以高音量随机播放1个文件。我找到了对类似问题的回复:How do I call an array value and use it to play a sound file?但我没有发现问题或回复在我的应用中足够清晰或非常有用。有没有人知道我可以创建mp3音频文件数组然后创建一个随机剪辑的方法?提前谢谢。

1 个答案:

答案 0 :(得分:0)

我能够创建NSArray并随机化使用此代码播放的mp3文件。 在我的ViewController.h中

`@property (strong, nonatomic) NSArray *soundsArray;

然后在viewDidLoad方法的ViewController.m中

self.soundsArray =[[NSArray alloc] initWithObjects:@"sound1", @"sound2", nil];

我使用

随机化了IBAction中的声音
NSUInteger index = arc4random_uniform(self.soundsArray.count);

    NSString *filePath = [self.guySoundsArray objectAtIndex:index];
    NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:filePath ofType: @"mp3"];
    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];