How to add sound to app using Xcode?

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

  1. GenoFloresAZ macrumors newbie

    Joined:
    Mar 15, 2014
    Location:
    Phoenix, Arizona
    #1
    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

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

    Code:
    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

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


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

Share This Page