views:

347

answers:

1

I'm coding in Objective-C for the iPhone and I am trying create an array that plays a series of sounds. For example the first time I press the button I want it play sound "0.wav", but the second time I want it to play "1.wav", then "2.wav", "3.wav", etc. Then when I've played a total of 14 sounds (up to "13.wav") I want the loop to start over playing with 0.wav. I've looked around Google and the Apple development documentation for almost 4 days without much luck. So if someone could help me generate a code for this that would be greatly appreciated and if you wouldn't mind could you attempt to explain the code briefly so that I can learn what each part does.

Thank you, Joey


EDIT

Okay I've gotten the Array part down (thanks to Thomas) however neither of us are sure how to implement the soundID from the array to the action where I play the sound and how to play that sound. I used the format Thomas used for his array below if that helps you with your answer.

Thanks, Joey

A: 

First, create the array of your different sounds and a variable to hold the current index:

NSArray *sounds = [[NSArray alloc] initWithObjects:@"0.wav", @"1.wav", nil]; // add all your files here
NSUInteger currentSound = 0;

Then, when you handle your button tapped event, go to the next sound, and play it. If it's at the end of your NSArray, go back to index 0:

currentSound++;
if (currentSound >= [sounds count]) {
    currentSound = 0;
}

// play the sound. Just call your own method here
[self playSoundWithFilename:[sounds objectAtIndex:currentSound]];
Thomas Müller
Where should I put each of those codes. I currently have the first one in "viewDidLoad" and the second one in my action I call when the button is pressed to play the sound. Which is a void. Should it be an IBAction? And do I need to declare anything in my header file or is it all pre-done in the frameworks.Also how do I play the sound file using the result I got from the array? I'm not sure how to use the current value of the array as the file soundID.
infiniteloop91
viewDidLoad is probably fine for the first part.Make sure to release the NSArray in dealloc.Your action can return void, I think you basically need IBAction to tell Interface Builder that it's an action. I understand that IBAction is equivalent to void.Not sure about playing the sound, I've got no experience with that.
Thomas Müller
Okay thanks for the help. It definitely helped a lot, I'll ask around about playing the sound.
infiniteloop91