2010-08-07 39 views
0

我想通過觸摸相應的UImageView來播放幾個audiofiles(* .wav)。每觸摸一次UIImageView就會改變一個「triggernumber」 - 選擇該視圖。音頻名稱存儲在一個數組中。但我無法弄清楚如何寫一個方法來獲得的名稱和遊戲權的聲音,而不使用與冗餘codesnippets一個選擇案例的方法來播放聲音:如何從NSArray獲取Soundfilenames並播放?

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


} 

或者這是一個完全錯誤的做法解決這個問題?

相關的第二個問題是:我怎麼能把這三個「如果([觸摸視圖] == IMG ...」代碼在更短的(選擇情況)語句

感謝您的回答?!

回答

0

嘗試......

[arrAnimalde objectAtIndex: trigAnimal] 

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

非常感謝它工作正常 - 它是如此容易... – FredIce 2010-08-09 13:01:12

+0

沒問題,但可能想檢查這個答案。 – Jordan 2010-08-09 20:04:31