iphone - 如何从NSArray获取声音文件名并播放?

标签 iphone xcode audio if-statement nsarray

我想通过触摸相应的UImageViews来播放几个音频文件(* .wav)。每次触摸UIImageView都会更改一个“触发编号”-选中该 View 。音频文件名称存储在数组中。但是我不知道如何编写一种方法来获取名称并播放正确的声音,而无需使用带有冗余代码片段的select case方法来播放声音:

#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?)语句中?

谢谢您的回答!

最佳答案

试试这个...

   [arrAnimalde objectAtIndex: trigAnimal]

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

关于iphone - 如何从NSArray获取声音文件名并播放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3432055/

相关文章:

iOS模拟器声音

ios - 呈现具有模糊效果的 View Controller

c++ - FLAC.框架错误

c# - 使用DirectSound设置不同的音频输出

matlab - 通过MATLAB中的fft进行卷积定理

javascript - 在没有 "Open in <app>"提示的情况下从 Safari 打开 iOS 应用程序

ios - 永远不应触发嵌套优化。这可能是由于自动布局工作发生在 NSISVariable 委托(delegate)回调中

ios - UIButton 改变位置

Android AudioTrack 流提前中断

iphone - iOS 6 MFMailComposeViewController : Only support RGBA or the White color space, 这个方法是 hack