如何从 NSArray 获取 Soundfilenames 并播放?
Posted
技术标签:
【中文标题】如何从 NSArray 获取 Soundfilenames 并播放?【英文标题】:How to get Soundfilenames from an NSArray and play? 【发布时间】:2010-08-07 21:11:22 【问题描述】:我想通过触摸相应的 UImageView 来播放多个音频文件 (*.wav)。每次触摸 UIImageView 都会更改一个“触发器编号”——选择哪个视图。音频文件名称存储在一个数组中。但是我不知道如何编写一个方法来获取名称并播放正确的声音,而不使用带有冗余 coden-ps 的 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" 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([touch view] == img...”代码放在更短的(select-case?)语句中?
【问题讨论】:
【参考方案1】:试试这个...
[arrAnimalde objectAtIndex: trigAnimal]
例如
AudioServicesCreateSystemSoundID ((CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource: [arrAnimalde objectAtIndex: trigAnimal] ofType: @"wav"]], &soundFileObject);
【讨论】:
没问题,但可能想检查一下是否已回答。以上是关于如何从 NSArray 获取 Soundfilenames 并播放?的主要内容,如果未能解决你的问题,请参考以下文章
如何从 NSArray 获取 Soundfilenames 并播放?