首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从NSArray中获取声音文件名并播放?

如何从NSArray中获取声音文件名并播放?
EN

Stack Overflow用户
提问于 2010-08-08 05:11:22
回答 1查看 288关注 0票数 0

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

代码语言:javascript
复制
#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 ((CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource: @"**here please the right name from the array**" ofType: @"wav"]], &soundFileObject);
    AudioServicesPlaySystemSound (self.soundFileObject);
    

}

或者这是一个完全错误的解决方法?

相关的第二个问题是:我如何才能把三个“if(触摸视图== img...)”更短的代码(select-case?)声明?

EN

回答 1

Stack Overflow用户

发布于 2010-08-08 05:45:37

试试这个。

代码语言:javascript
复制
   [arrAnimalde objectAtIndex: trigAnimal]

例如:

代码语言:javascript
复制
 AudioServicesCreateSystemSoundID ((CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource: [arrAnimalde objectAtIndex: trigAnimal] ofType: @"wav"]], &soundFileObject);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3432055

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档