How to add sound to app using Xcode?

Discussion in 'iOS Programming' started by GenoFloresAZ, Mar 23, 2014.

  1. GenoFloresAZ macrumors newbie

    Mar 15, 2014
    Phoenix, Arizona
    I'm making a game for iOS, everything is finished except sounds. The app already plays a sound when buttons are pressed, but I need another sound file to be played when the bee (main UIImage character) comes in contact with the coin (object of the game is to catch these coins). And also I need another sound file to be played when the bee comes in contact with one of the two obstacles. How do I go on about doing this?
  2. RagingGoat macrumors 6502

    Jun 21, 2010
    In your method where you determine what to do when contact is made you can do something like this:

    NSError *error = nil;
        NSURL *pointURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/sound.wav", [[NSBundle mainBundle]resourcePath]]];
        audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:pointURL error:&error];
        [audioPlayer prepareToPlay];
        [audioPlayer play];
  3. Duncan C macrumors 6502a

    Duncan C

    Jan 21, 2008
    Northern Virginia
    That code will work but I would be inclined to use the NSBundle method URLForResource:withExtension instead:

    NSURL *pointURL = [[NSBundle mainBundle]  URLForResource: @"sound" 
      withExtension: @"wav"];

Share This Page