views:

14

answers:

1

Hello, I want to play several audiofiles (*.wav) by touching the appropriate UImageViews. Each touching an UIImageView changes an "triggernumber" - wich view is selected. The audiofile names are stored in an array. But I can't figure out how to write a method to get the names and play the right sounds without using a select case method with redundant codesnippets to play the sounds:

#pragma mark -

-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
//Soundfile names in array arrAnimalde
arrAnimalde = [NSArray arrayWithObjects:@"Hund" ,@"Schwein" ,@"Ferkel", nil];

// here are some Views and their triggers
if([touch view] == img_hund){
    trigAnimal = 0;
    }

if([touch view] == img_schwein){
    trigAnimal = 1;
    }

if([touch view] == img_ferkel){
    trigAnimal = 2;
      }

        /* here I'd like to have a method to send the triggernumber and get the 
soundnameposition in the array to play the appropriate sound e. g. "Hund.wav" wich is the first entry
 in the array : I tried something with "self.langKeys objectAtIndex:arrAnimalde" instead of the string in the following code but it 
does'nt work
*/

        //Sound
    AudioServicesCreateSystemSoundID ((CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource: @"**here please the right name from the array**" ofType: @"wav"]], &soundFileObject);
    AudioServicesPlaySystemSound (self.soundFileObject);


}

Or is this a completely wrong approach to solve it?

Related second question is: How can I put the three "if([touch view] == img..." code in a shorter (select-case?) statement?

Thank You for answering!

A: 

Try this...

   [arrAnimalde objectAtIndex: trigAnimal]

e.g.

 AudioServicesCreateSystemSoundID ((CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource: [arrAnimalde objectAtIndex: trigAnimal] ofType: @"wav"]], &soundFileObject);
Jordan
Thanks a lot! It works fine - and it's so easy...
FredIce
No problem, but might want to check this as answered though.
Jordan