PDA

View Full Version : simple audio help




AdamRock
May 14, 2011, 08:30 PM
i'm currently making an application and one part has a button and when clicked plays a sound, how do i make it that when the user clicks the button it plays, but if they click it and its half way done playing and they click it again it restarts to audio?

heres the code;

-(IBAction)pushButton6 {
NSString *path =[[NSBundle mainBundle]
pathForResource:@"Aah" ofType:@"wav"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];

}



firewood
May 14, 2011, 10:28 PM
First thing to do is to retain your audio player object using something other than a local variable.

When you figure out where to retain and how to access that object, you will find that there are other messages you can sent to it listed in the extensive AVAudioPlayer documentation.

jnoxx
May 15, 2011, 06:55 AM
Someone asked this last week, and I shared code, not to be rude, but there is a Search button for not duplicating posts too much.

AdamRock
May 15, 2011, 11:12 AM
Someone asked this last week, and I shared code, not to be rude, but there is a Search button for not duplicating posts too much.

i actually did use the search feature before making this thread, and the post you made a week ago was not the same problem that i'm having, and there's no other threads on macrumors regarding this.

nickculbertson
May 15, 2011, 02:59 PM
You must release the first sound before you start the new one. When in doubt try youtube.

here (http://www.youtube.com/watch?v=uq3l9KRLixU)

Nick

AdamRock
May 15, 2011, 10:01 PM
You must release the first sound before you start the new one. When in doubt try youtube.

here (http://www.youtube.com/watch?v=uq3l9KRLixU)

Nick

that video doesn't help me that much...

i need my 1 button to be able to cease all sounds before playing its new song.

tried to implement a [player stop]; before the [player play]; but that didn't work.

i'm starting to think apple likes making things harder then they are?

firewood
May 16, 2011, 01:24 AM
tried to implement a [player stop]; before the [player play]; but that didn't work.

Which player object did you try to stop? The old one? Or the new one?

Print out the object pointers (or inspect them in the debugger) to make sure you are using the correct one.

nickculbertson
May 16, 2011, 06:24 AM
The solution is in the video, your just not finding it. I said you need to release the sound, not stop it. (how is the audio released in the video?)

firewood
May 16, 2011, 03:03 PM
i'm starting to think apple likes making things harder then they are?

Absolutely.

Apple assumes that all iPhone developers are fairly intelligent and will carefully read the copious amounts of documentation and details available. Apple then creates their iOS dev materials accordingly.

It's Apple's pseudo-IQ test to reduce the number of clueless or mediocre developers who manage to create and submit working apps.

It also means Apple can spend more of their time creating advanced stuff for the good developers to make their apps even better.

AdamRock
May 19, 2011, 07:38 AM
The solution is in the video, your just not finding it. I said you need to release the sound, not stop it. (how is the audio released in the video?)

the guy in the vid has 1 button for play 1 for stop, i need 1 button to do both.

firewood
May 19, 2011, 02:33 PM
the guy in the vid has 1 button for play 1 for stop, i need 1 button to do both.

Your button needs a model object (or state variable) that can save the current state of activity, or the action to do next. Look up "state machines", "state variables", and "conditional statements in C". Stuff that's mentioned in many basic computer science and programming textbooks.

nickculbertson
May 20, 2011, 08:34 AM
From the video.


-(IBAction)pushButton6 {
NSString *path =[[NSBundle mainBundle]
pathForResource:@"Aah" ofType:@"wav"];
if (theAudio)[theAudio release];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];

}