Search code examples
xcodeios6

Master-Detail and sounds


I'm new to Objective-C. My problem is I have an app in a Master-Detail design that has a table listing English words with their Welsh translations. That works great, but I also would like to add sound via a UIButton for the pronunciation. I know how to make one sound play, but how do I make the corresponding sound for the correct word play when I hit the button?

My table is as follows:

-(void)createWordData {
    NSMutableArray *nounWords;
    NSMutableArray *verbWords;

    _wordSections=@[@"Nouns", @"Verbs"];

    nounWords=[[NSMutableArray alloc] init];
    verbWords=[[NSMutableArray alloc] init];

    [nounWords addObject:@{@"English":@"girl",@"Cymraeg":@"y ferch (f.)",@"Sound":@"Merch"}];
    [nounWords addObject:@{@"English":@"boy",@"Cymraeg":@"y bachgen (m.)",@"Sound":@"Bachgen"}];
    [nounWords addObject:@{@"English":@"cat",@"Cymraeg":@"y gath (f.)",@"Sound":@"Cath"}];
    [nounWords addObject:@{@"English":@"dog",@"Cymraeg":@"y ci (m.)",@"Sound":@"Ci"}];

    [verbWords addObject:@{@"English":@"walk",@"Cymraeg":@"cerdded",@"Sound":@"Cerdded"}];
    [verbWords addObject:@{@"English":@"run",@"Cymraeg":@"rhedeg",@"Sound":@"Rhedeg"}];
    [verbWords addObject:@{@"English":@"play",@"Cymraeg":@"chwarae",@"Sound":@"Chwarae"}];
    [verbWords addObject:@{@"English":@"sleep",@"Cymraeg":@"cysgu",@"Sound":@"Cysgu"}];

    _wordData=@[nounWords,verbWords];
}

The play action is as follows:

- (IBAction)pronunciation:(id)sender {

    SystemSoundID soundID;
    NSString *soundFile = [[NSBundle mainBundle]
                           pathForResource:@"Cath" ofType:@"wav"];

    AudioServicesCreateSystemSoundID((__bridge CFURLRef)
                                     [NSURL fileURLWithPath:soundFile]
                                     , &soundID);
    AudioServicesPlayAlertSound(soundID);
}

Solution

  • In your pronunciation: method, instead of hard-coding @"Cath", make the choice of sound depend upon the facts about sender (or some other fact about the current state of things that determines which word is intended by the user).

    By the way, making this sound an alert sound is probably not at all the best way to produce it. (And in any case you may be leaking your sound.) You should be using an AVAudioPlayer.