如何从NSArray获取Soundfilenames并播放?

时间:2010-08-07 21:11:22

标签: iphone xcode audio if-statement nsarray

我想通过触摸相应的UImageViews来播放几个音频文件(* .wav)。每次触摸UIImageView都会更改“触发器号” - 选择视图。音频文件名称存储在一个数组中。但我无法弄清楚如何编写一个方法来获取名称并播放正确的声音而不使用带有冗余代码片段的选择案例方法来播放声音:

#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);


}

或者这是解决它的完全错误的方法吗?

相关的第二个问题是:如何将三个“if([touch view] == img ...”代码放在一个较短的(select-case?)语句中?

感谢您的回答!

1 个答案:

答案 0 :(得分:0)

试试这个......

   [arrAnimalde objectAtIndex: trigAnimal]

e.g。

 AudioServicesCreateSystemSoundID ((CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource: [arrAnimalde objectAtIndex: trigAnimal] ofType: @"wav"]], &soundFileObject);