简体   繁体   English

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

[英]Creating an array of mp3 audio files in iOS and playing them at random with a UIButton

I am attempting to create a prediction making iOS 5 iPhone App that will play a short (1-15 second long) Random mp3 audio clip from an array that can be triggered by a UIButton's touchUpInside event. 我正在尝试创建一个预测的iOS 5 iPhone App,它将从可以由UIButton的touchUpInside事件触发的数组中播放短(1-15秒长)随机mp3音频剪辑。 I want to create an array with the mp3 files and then create a method that when called will play 1 file at random at a high volume. 我想创建一个包含mp3文件的数组,然后创建一个方法,该方法在被调用时会随机播放大量的1个文件。 I have found a response to a similar question: How do I call an array value and use it to play a sound file? 我找到了对类似问题的答复: 如何调用数组值并使用它播放声音文件? But I did not find the question or response to be clear enough or very helpful in my app. 但是我发现问题或答案在我的应用程序中不够清楚或没有太大帮助。 Does anybody know of the most efficient way I can go about creating the mp3 audio file array and then creating a method that will play a random clip back? 有人知道我可以最有效的方法来创建mp3音频文件数组,然后创建可以播放随机剪辑的方法吗? Thanks in advance. 提前致谢。

I was able to create the NSArray and randomize the mp3 file played using this code. 我能够创建NSArray并使用该代码随机播放mp3文件。 In my ViewController.h 在我的ViewController.h中

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

Then in my ViewController.m in the viewDidLoad method 然后在我的ViewController.m中的viewDidLoad方法中

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

And I randomized the sounds in an IBAction using, 然后,我使用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];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM