How to Add Sound to iPhone App?

Discussion in 'iOS Programming' started by andreab35, Jan 31, 2009.

  1. andreab35 macrumors 6502a

    andreab35

    Joined:
    May 29, 2008
    Location:
    USA
    #1
    Please... please don't flame me! I'm still trying to learn about iPhone development! :eek:

    Alrighty, I finally got the interface of my app to built up. Now to only thing I want to do is to add sound to my application.

    What I would like to do is when a person taps a button, I want that tapped button to start playing a sound. When a person then taps another button, I want it to play a different sound.

    Can someone kindly guide me through how to do this?
    Any help is greatly appreciated!
    Thank you for understanding! :eek:
     
  2. MasterObiWan macrumors newbie

    Joined:
    Jan 12, 2009
    #2
    Do you need to play just a sound file or music? Because if you want to know about sound files I know how.
     
  3. andreab35 thread starter macrumors 6502a

    andreab35

    Joined:
    May 29, 2008
    Location:
    USA
    #3
    Wirelessly posted (iPhone: Mozilla/5.0 (iPhone; U; CPU iPhone OS 2_2_1 like Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5H11 Safari/525.20)

    It's actually playing a music file that I am looking to do.
     
  4. MasterObiWan macrumors newbie

    Joined:
    Jan 12, 2009
    #4
    I've never tried it before but this might help, someone please help explain it better:

    You need to use AVFoundation framework which includes a class called AVAudioPlayer. mp3 file should be in your resource group.

    Code:
    //get the audio file path
    NSString* path;
    NSURL* url;
    
    path = [[NSBundle mainBundle] pathForResource:@"music" ofType:@"mp3"];
    url = [NSURL fileURLWithPath:path];
    
    //initilize AVAudioPlayer
    AVAudioPlayer* player;
    player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
    
    
    Then use the following methods to control your sound

    - (BOOL)play
    - (void)pause
    -(void)stop

    Edit: just tried it and it worked. Good luck with your app.
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    What is the length of the music in these files?
     
  6. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #6
    The answer may depend on exactly what type of sound, what format of sound file (wav, caf, midi, mp3), how long a sound, how fast you want it to start, whether you want to vary the sound (pitch, duration, timbre), what you want to do when the user taps two buttons before the first sound ends, what if the user is already playing ipod music in the background, how much memory you want to use or not use, and etc.
     
  7. andreab35 thread starter macrumors 6502a

    andreab35

    Joined:
    May 29, 2008
    Location:
    USA
    #7
    Thank you MasterObiWan! I will try that out in XCode and see if I will be able to get that to work.

    Well, the music I need to play needs to loop and continuously play until the person taps the button to stop it. I really don't know what to do for that, but I may be able to give it a stab and try it out to see if I can get somewhere.

    Here is how I would like it to go:
    - Person taps button an music starts playing normally (music does not stop, it needs to keep looping)
    - Person decides to tap on another button, and the previous music starts and the other tapped button starts another type of music.
    - When person opens app their iPod Touch/iPhone music must fade away and stop.
    - I am looking for it not to be too memory intensive.
    - It will most likely be in mp3 format.

    Thank you so much guys!
     
  8. trigga71 macrumors newbie

    Joined:
    Mar 12, 2009
    #8
    i know nothing about xcode, but im getting "error: 'play' undeclared (first use in this function)" error. Could someone help me?

    I believe i have the AVFoundation importing correct. So should it know what the "play" command is?
     
  9. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    Can you give us a code snippet from line causing this error, and perhaps a few lines before?
     
  10. SuperRoOt macrumors newbie

    Joined:
    Apr 25, 2009
    Location:
    Ohio
    #10
    Hey I need to add sounds, .wav files, to start when certain screens open. Can you help me out with this? Thanks!
     

Share This Page