How to get Soundfilenames from an NSArray and play?
I want to play several audiofiles (*.wav) by touching the appropriate UImageViews. Each touching an UIImageView changes an "triggernumber" - which 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" which 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 ((CFU开发者_开发知识库RLRef)[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?
Try this...
[arrAnimalde objectAtIndex: trigAnimal]
e.g.
AudioServicesCreateSystemSoundID ((CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource: [arrAnimalde objectAtIndex: trigAnimal] ofType: @"wav"]], &soundFileObject);
精彩评论