只需按一下按钮,我就会在不同的if语句中将声音文件附加到我的应用中。出于某种原因,一旦按下按钮,其中一个声音总是播放(谢谢),我无法找到音源。有时,它与现有声音重叠。 这是我的代码:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
weather.enabled=NO;
weather.hidden=YES;
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)go:(id)sender {
NSString *string =text.text;
if ([string rangeOfString:@"friend"].location == NSNotFound) {
}
else {label.text = @"You're my best friend";
SystemSoundID SoundID;
NSString *soundfile= [[NSBundle mainBundle] pathForResource:@"You're my best friend" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundfile], &SoundID );
AudioServicesPlaySystemSound(SoundID);
NSLog(@"sound played");
}
if ([string rangeOfString:@"hat are you doing"].location == NSNotFound) {
}
else {label.text = @"I'm talking to you";
SystemSoundID SoundID;
NSString *soundfile= [[NSBundle mainBundle] pathForResource:@"I'm talking to you" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundfile], &SoundID );
AudioServicesPlaySystemSound(SoundID);
NSLog(@"sound played");
}
if ([string rangeOfString:@"favorite snack"].location == NSNotFound) {
}
else {label.text = @"Ooooh. I love chips.";
SystemSoundID SoundID;
NSString *soundfile= [[NSBundle mainBundle] pathForResource:@"chips" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundfile], &SoundID );
AudioServicesPlaySystemSound(SoundID);
NSLog(@"sound played");
}
if ([string rangeOfString:@"was your day"].location == NSNotFound) {
}
else {label.text = @"It was techy";
SystemSoundID SoundID;
NSString *soundfile= [[NSBundle mainBundle] pathForResource:@"it was techy" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundfile], &SoundID );
AudioServicesPlaySystemSound(SoundID);
NSLog(@"sound played");
}
if ([string rangeOfString:@"weather"].location == NSNotFound) {
}
else {label.text = @"Cloudy with a chance of meatballs, GET It? But really, click below";
weather.enabled=YES;
weather.hidden=NO;
SystemSoundID SoundID;
NSString *soundfile= [[NSBundle mainBundle] pathForResource:@"Weather" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundfile], &SoundID );
AudioServicesPlaySystemSound(SoundID);
NSLog(@"sound played");
}
if ([string rangeOfString:@"the time"].location == NSNotFound) {
}
else {label.text = @"I'm not sure. There must be a clock somewhere near.";
SystemSoundID SoundID;
NSString *soundfile= [[NSBundle mainBundle] pathForResource:@"time" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundfile], &SoundID );
AudioServicesPlaySystemSound(SoundID);
NSLog(@"sound played");
}
if ([string rangeOfString:@"your birthday"].location == NSNotFound) {
}
else {label.text = @"I was born one day in an Apple Computer. I can't seem to remember when that was.";
SystemSoundID SoundID;
NSString *soundfile= [[NSBundle mainBundle] pathForResource:@"Your birthday" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundfile], &SoundID );
AudioServicesPlaySystemSound(SoundID);
NSLog(@"sound played");
}
if ([string rangeOfString:@"ello"].location == NSNotFound) {
}
else {label.text = @"Hi"; SystemSoundID SoundID;
NSString *soundfile= [[NSBundle mainBundle] pathForResource:@"hi" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundfile], &SoundID );
AudioServicesPlaySystemSound(SoundID);
NSLog(@"sound played");
}
if ([string rangeOfString:@"yes"].location == NSNotFound) {
}
else {label.text = @"Ok"; SystemSoundID SoundID;
NSString *soundfile= [[NSBundle mainBundle] pathForResource:@"ok" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundfile], &SoundID );
AudioServicesPlaySystemSound(SoundID);
NSLog(@"sound played");
}
if ([string rangeOfString:@"sure"].location == NSNotFound) {
}
else {label.text = @"Ok"; SystemSoundID SoundID;
NSString *soundfile= [[NSBundle mainBundle] pathForResource:@"ok" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundfile], &SoundID );
AudioServicesPlaySystemSound(SoundID);
NSLog(@"sound played");
}
if ([string rangeOfString:@"yeah"].location == NSNotFound) {
}
else {label.text = @"Ok";
SystemSoundID SoundID;
NSString *soundfile= [[NSBundle mainBundle] pathForResource:@"ok" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundfile], &SoundID );
AudioServicesPlaySystemSound(SoundID);
NSLog(@"sound played");}
if ([string rangeOfString:@"do you do"].location == NSNotFound) {
}
else {label.text = @"I am a student";
SystemSoundID SoundID;
NSString *soundfile= [[NSBundle mainBundle] pathForResource:@"im a student" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundfile], &SoundID );
AudioServicesPlaySystemSound(SoundID);
NSLog(@"sound played");}
if ([string rangeOfString:@"awesome"].location == NSNotFound) {
}
else {label.text = @"Thank You";SystemSoundID SoundID;
NSString *soundfile= [[NSBundle mainBundle] pathForResource:@"Thanks" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundfile], &SoundID );
AudioServicesPlaySystemSound(SoundID);
NSLog(@"sound played");}
if ([string rangeOfString:@"hi"].location == NSNotFound) {
}
else {label.text = @"Hi"; SystemSoundID SoundID;
NSString *soundfile= [[NSBundle mainBundle] pathForResource:@"hi" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundfile], &SoundID );
AudioServicesPlaySystemSound(SoundID);
NSLog(@"sound played");
}
if ([string rangeOfString:@"favorite color"].location == NSNotFound) {
}
else {label.text = @"My favorite color is Orange";
SystemSoundID SoundID;
NSString *soundfile= [[NSBundle mainBundle] pathForResource:@"my favorite color" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundfile], &SoundID );
AudioServicesPlaySystemSound(SoundID);
NSLog(@"sound played");}
if ([string rangeOfString:@"Hi"].location == NSNotFound) {
}
else {label.text = @"Hello";
SystemSoundID SoundID;
NSString *soundfile= [[NSBundle mainBundle] pathForResource:@"hi" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundfile], &SoundID );
AudioServicesPlaySystemSound(SoundID);
NSLog(@"sound played");}
if ([string rangeOfString:@"sup"].location == NSNotFound) {
}
else {label.text = @"Nothing much";
SystemSoundID SoundID;
NSString *soundfile= [[NSBundle mainBundle] pathForResource:@"nothing much" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundfile], &SoundID );
AudioServicesPlaySystemSound(SoundID);
NSLog(@"sound played");}
if ([string rangeOfString:@"how are you"].location == NSNotFound) {
}
else {label.text = @"I'm fine. Thank You";
SystemSoundID SoundID;
NSString *soundfile= [[NSBundle mainBundle] pathForResource:@"im fine" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundfile], &SoundID );
AudioServicesPlaySystemSound(SoundID);
NSLog(@"sound played");}
if ([string rangeOfString:@"How are you"].location == NSNotFound) {
}
else {label.text = @"I'm fine. Thank You";
SystemSoundID SoundID;
NSString *soundfile= [[NSBundle mainBundle] pathForResource:@"im fine" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundfile], &SoundID );
AudioServicesPlaySystemSound(SoundID);
NSLog(@"sound played");}
if ([string rangeOfString:@"Cool"].location == NSNotFound) {
}
else {label.text = @"Thanks";
SystemSoundID SoundID;
NSString *soundfile= [[NSBundle mainBundle] pathForResource:@"thanks informal" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundfile], &SoundID );
AudioServicesPlaySystemSound(SoundID);
NSLog(@"sound played");}
if ([string rangeOfString:@"cool"].location == NSNotFound) {
}
else {label.text = @"Thanks";
SystemSoundID SoundID;
NSString *soundfile= [[NSBundle mainBundle] pathForResource:@"thanks informal" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundfile], &SoundID );
AudioServicesPlaySystemSound(SoundID);
NSLog(@"sound played");}
if ([string rangeOfString:@"should I call you"].location == NSNotFound) {
}
else {label.text = @"You can call me Cara";
SystemSoundID SoundID;
NSString *soundfile= [[NSBundle mainBundle] pathForResource:@"you can call me cara" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundfile], &SoundID );
AudioServicesPlaySystemSound(SoundID);
NSLog(@"sound played");}
if ([string rangeOfString:@"nice name"].location == NSNotFound) {
}
else {label.text = @"Thanks";
SystemSoundID SoundID;
NSString *soundfile= [[NSBundle mainBundle] pathForResource:@"thanks informal" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundfile], &SoundID );
AudioServicesPlaySystemSound(SoundID);
NSLog(@"sound played");}
if ([string rangeOfString:@"you born"].location == NSNotFound) {
}
else {label.text = @"I was born in an Apple Computer";
SystemSoundID SoundID;
NSString *soundfile= [[NSBundle mainBundle] pathForResource:@"born in apple" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundfile], &SoundID );
AudioServicesPlaySystemSound(SoundID);
NSLog(@"sound played");}
if ([string rangeOfString:@"language"].location == NSNotFound) {
}
else {label.text = @"I speak english";
SystemSoundID SoundID;
NSString *soundfile= [[NSBundle mainBundle] pathForResource:@"ispeakenglish" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundfile], &SoundID );
AudioServicesPlaySystemSound(SoundID);
NSLog(@"sound played");}
if ([string rangeOfString:@"nice name"].location == NSNotFound) {
}
else {label.text = @"Thanks";
SystemSoundID SoundID;
NSString *soundfile= [[NSBundle mainBundle] pathForResource:@"thanks informal" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundfile], &SoundID );
AudioServicesPlaySystemSound(SoundID);
NSLog(@"sound played");}
if ([string rangeOfString:@"ok"].location == NSNotFound) {
}
else {label.text = @"Great";
SystemSoundID SoundID;
NSString *soundfile= [[NSBundle mainBundle] pathForResource:@"grt" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundfile], &SoundID );
AudioServicesPlaySystemSound(SoundID);
NSLog(@"sound played");}
if ([string rangeOfString:@"old are you"].location == NSNotFound) {
}
else {label.text = @"That's a little personal. Can we please talk about something else?";
SystemSoundID SoundID;
NSString *soundfile= [[NSBundle mainBundle] pathForResource:@"thatspersonal" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundfile], &SoundID );
AudioServicesPlaySystemSound(SoundID);
NSLog(@"sound played");}
if ([string rangeOfString:@"free time"].location == NSNotFound) {
}
else{label.text = @"I like to sleep.";
SystemSoundID SoundID;
NSString *soundfile= [[NSBundle mainBundle] pathForResource:@"liketosleep" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundfile], &SoundID );
AudioServicesPlaySystemSound(SoundID);
NSLog(@"sound played");}
if ([string rangeOfString:@"nice name"].location == NSNotFound) {
}
else {label.text = @"Thanks";
SystemSoundID SoundID;
NSString *soundfile= [[NSBundle mainBundle] pathForResource:@"thanks informal" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundfile], &SoundID );
AudioServicesPlaySystemSound(SoundID);
NSLog(@"sound played");}
if ([string rangeOfString:@"name"].location == NSNotFound) {
}
else {label.text = @"My name is Cara";
SystemSoundID SoundID;
NSString *soundfile= [[NSBundle mainBundle] pathForResource:@"you can call me cara" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundfile], &SoundID );
AudioServicesPlaySystemSound(SoundID);
NSLog(@"sound played");
}
if ([string rangeOfString:@"thank"].location == NSNotFound) {
}
else {label.text = @"You're very welcome";
SystemSoundID SoundID;
NSString *soundfile= [[NSBundle mainBundle] pathForResource:@"welcome" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundfile], &SoundID );
AudioServicesPlaySystemSound(SoundID);
NSLog(@"sound played");}
if ([string rangeOfString:@"thank"].location == NSNotFound) {
}
else {label.text = @"You're very welcome";
SystemSoundID SoundID;
NSString *soundfile= [[NSBundle mainBundle] pathForResource:@"welcome" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundfile], &SoundID );
AudioServicesPlaySystemSound(SoundID);
NSLog(@"sound played");}
if ([string rangeOfString:@"thank"].location == NSNotFound) {
}
else {label.text = @"You're very welcome";
SystemSoundID SoundID;
NSString *soundfile= [[NSBundle mainBundle] pathForResource:@"welcome" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundfile], &SoundID );
AudioServicesPlaySystemSound(SoundID);
NSLog(@"sound played");}
if ([string rangeOfString:@"you live"].location == NSNotFound) {
}
else {label.text = @"I live right here on your device";
SystemSoundID SoundID;
NSString *soundfile= [[NSBundle mainBundle] pathForResource:@"live" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundfile], &SoundID );
AudioServicesPlaySystemSound(SoundID);
NSLog(@"sound played");}
if ([string rangeOfString:@"love you"].location == NSNotFound) {
}
else {label.text = @"You're so nice";}
SystemSoundID SoundID;
NSString *soundfile= [[NSBundle mainBundle] pathForResource:@"Thanks" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundfile], &SoundID );
AudioServicesPlaySystemSound(SoundID);
NSLog(@"sound played");
if ([string rangeOfString:@"amazing"].location == NSNotFound) {
}
else {label.text = @"Thank you";
SystemSoundID SoundID;
NSString *soundfile= [[NSBundle mainBundle] pathForResource:@"thanks" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundfile], &SoundID );
AudioServicesPlaySystemSound(SoundID);
NSLog(@"sound played");}
if ([string rangeOfString:@"hate"].location == NSNotFound) {
}
else {label.text = @"I'm sorry. I do my best";
SystemSoundID SoundID;
NSString *soundfile= [[NSBundle mainBundle] pathForResource:@"imsorry" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundfile], &SoundID );
AudioServicesPlaySystemSound(SoundID);
NSLog(@"sound played");}
if ([string rangeOfString:@"homework"].location == NSNotFound) {
}
else {label.text = @"Well, I don't think I'm smart enough. But you are!!";
SystemSoundID SoundID;
NSString *soundfile= [[NSBundle mainBundle] pathForResource:@"smarthw" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundfile], &SoundID );
AudioServicesPlaySystemSound(SoundID);
NSLog(@"sound played");
}
if ([string rangeOfString:@"you like school"].location == NSNotFound) {
}
else {label.text = @"Umm, sometimes";
SystemSoundID SoundID;
NSString *soundfile= [[NSBundle mainBundle] pathForResource:@"sometimes" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundfile], &SoundID );
AudioServicesPlaySystemSound(SoundID);
NSLog(@"sound played");}
if ([string rangeOfString:@"have any siblings"].location == NSNotFound) {
}
else {label.text = @"No. I'm an only child.";
SystemSoundID SoundID;
NSString *soundfile= [[NSBundle mainBundle] pathForResource:@"onlychild" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundfile], &SoundID );
AudioServicesPlaySystemSound(SoundID);
NSLog(@"sound played");}
if ([string rangeOfString:@" you have friends"].location == NSNotFound) {
}
else {label.text = @"Yes. You're my best friend";
SystemSoundID SoundID;
NSString *soundfile= [[NSBundle mainBundle] pathForResource:@"You're my best friend" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundfile], &SoundID );
AudioServicesPlaySystemSound(SoundID);
NSLog(@"sound played");
}
if ([string rangeOfString:@"be my friend"].location == NSNotFound) {
}
else {label.text = @"Sure";
SystemSoundID SoundID;
NSString *soundfile= [[NSBundle mainBundle] pathForResource:@"sure 3.31.32 PM " ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundfile], &SoundID );
AudioServicesPlaySystemSound(SoundID);
NSLog(@"sound played");}
if ([string rangeOfString:@"Good morning"].location == NSNotFound) {
}
else {label.text = @"Good morning";
SystemSoundID SoundID;
NSString *soundfile= [[NSBundle mainBundle] pathForResource:@"morning" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundfile], &SoundID );
AudioServicesPlaySystemSound(SoundID);
NSLog(@"sound played");}
if ([string rangeOfString:@"Good night"].location == NSNotFound) {
}
else {label.text = @"Good Night";}
if ([string rangeOfString:@"Good Night"].location == NSNotFound) {
}
else {label.text = @"Good Night";}
if ([string rangeOfString:@"Good Morning"].location == NSNotFound) {
}
else {label.text = @"Good morning";}
if ([string rangeOfString:@"Good morning"].location == NSNotFound) {
}
else {label.text = @"Good morning";}
if ([string rangeOfString:@"favorite tv show"].location == NSNotFound) {
}
else {label.text = @"I love the Simpsons. It's so funny. Have you heard about it?";}
if ([string rangeOfString:@"favorite TV show"].location == NSNotFound) {
}
else {label.text = @"I love the Simpsons. It's so funny. Have you heard of it?";}
if ([string rangeOfString:@"favorite sport"].location == NSNotFound) {
}
else {label.text = @"My favorite sport is soccer. What's your favorite sport?";}
if ([string rangeOfString:@"baseball"].location == NSNotFound) {
}
else {label.text = @"Nice";}
if ([string rangeOfString:@"basketball"].location == NSNotFound) {
}
else {label.text = @"Nice";}
if ([string rangeOfString:@"soccer"].location == NSNotFound) {
}
else {label.text = @"Cool";}
if ([string rangeOfString:@"swimming"].location == NSNotFound) {
}
else {label.text = @"Cool";}
if ([string rangeOfString:@"football"].location == NSNotFound) {
}
else {label.text = @"Cool";}
if ([string rangeOfString:@"favorite food"].location == NSNotFound) {
}
else {label.text = @"Chips. Get it?";}
if ([string rangeOfString:@"No"].location == NSNotFound) {
}
else {label.text = @"Oh";}
if ([string rangeOfString:@"music do you like"].location == NSNotFound) {
}
else {label.text = @"I like to listen to the band Imagine Dragons";}
if ([string rangeOfString:@"What's up"].location == NSNotFound) {
}
else {label.text = @"The Sky. GET IT?. I'm just messing.";}
if ([string rangeOfString:@"What's up"].location == NSNotFound) {
}
else {label.text = @"The Sky. GET IT?. I'm just messing.";}
if ([string rangeOfString:@"what's up"].location == NSNotFound) {
}
else {label.text = @"The Sky. GET IT?. I'm just messing.";}
if ([string rangeOfString:@"favorite sport"].location == NSNotFound) {
}
else {label.text = @"I love soccer. What sport do you like?";}
if ([string rangeOfString:@"favorite song"].location == NSNotFound) {
}
else {label.text = @"I love to listen to Chimes by Hudson Mohawke.";}
}
- (IBAction)weather:(id)sender {
weather.enabled=NO;
weather.hidden=YES;
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.weather.com/weather/today/l/USNJ0413:1:US"]];
}
答案 0 :(得分:0)
使用更清洁方法的示例代码:
// not all values present, just a sample here
NSDictionary *stringsToSounds = @{@"friend":@"You're my best friend", @"hat are you doing":@"I'm talking to you", @"chips":@"chipds"};
NSDictionary *stringsToLabels = @{@"friend":@"You're my best friend", @"hat are you doing":@"I'm talking to you", @"chips":@"Ooooh. I love chips."};
for(NSString *s in stringsToSounds) {
if([string rangeOfString:s] != NSNotFound) {
if(stringsToLabels[s]) {
[label setText:stringsToLabels[s]];
}
[self playSound:stringsToSounds[s]];
break;
}
}
和方法playSound看起来像:
-
(void) playSound(NSString *) soundFileName {
SystemSoundID SoundID;
NSString *soundfile= [[NSBundle mainBundle] pathForResource:soundFileName ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundfile], &SoundID );
AudioServicesPlaySystemSound(SoundID);
NSLog(@"sound played");
}
答案 1 :(得分:0)
正如其他海报建议的那样,您可以使用字典来查找要为不同标签值播放的声音。代码可能如下所示:
-(void) playSoundForLabelText: (NSString *) text
{
//Build a dictionary where each key is the string to search for, and the value is the name of the sound to play.
NSDictionary *soundFileNames = @{@"I was born one day in an Apple Computer. I can't seem to remember when that was.": @"Your birthday",
@"Ooooh. I love chips.": @"chips",
@"Cloudy with a chance of meatballs, GET It? But really, click below": @"Weather"};
//Attempt to fetch a sound file name for the key in "text"
NSString *soundName = soundFileNames[text];
if (soundName != nil)
{
NSString *soundfile= [[NSBundle mainBundle] pathForResource: soundName ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundfile], &SoundID );
AudioServicesPlaySystemSound(SoundID);
NSLog(@"Played sound %@", soundName);
}
else
NSLog(@"No sound found for key %@", text);
}
然后你将这个方法用于这样的代码:
NSString *labelText = label.text;
[self playSoundForLabelText: labelText];
此代码未经过优化。它创建了每次都将标签文本映射到声音文件名的字典,这是低效的,但它为您提供了一般的想法。
BTW,AVAudioPlayer通常比AudioServicesCreateSystemSoundID更适合播放声音。在Xcode文档中搜索AVAudioPlayer以了解更多信息。