Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

andreab35

macrumors 6502a
Original poster
May 29, 2008
825
0
USA
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:
 
Do you need to play just a sound file or music? Because if you want to know about sound files I know how.
 
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.
 
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.
 
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.
 
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!
 
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?
 
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?
Can you give us a code snippet from line causing this error, and perhaps a few lines before?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.