Kevin Jones
Kevin Jones

Reputation: 23

Playing random sound with Xcode

Im planing on using this code to make random sounds play with an action, not exactly sure on how to name my sound files. Thanks for any help.

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];

Upvotes: 1

Views: 992

Answers (1)

lnafziger
lnafziger

Reputation: 25740

Name them like this:

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

Upvotes: 3

Related Questions