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

AdamRock

macrumors 6502a
Original poster
Aug 30, 2010
712
1
Toronto
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;
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];
	
}
 
Last edited by a moderator:

firewood

macrumors G3
Jul 29, 2003
8,106
1,343
Silicon Valley
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.
 

AdamRock

macrumors 6502a
Original poster
Aug 30, 2010
712
1
Toronto
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.
 

AdamRock

macrumors 6502a
Original poster
Aug 30, 2010
712
1
Toronto
You must release the first sound before you start the new one. When in doubt try youtube.

here

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

macrumors G3
Jul 29, 2003
8,106
1,343
Silicon Valley
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

macrumors regular
Nov 19, 2010
226
0
Nashville, TN
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

macrumors G3
Jul 29, 2003
8,106
1,343
Silicon Valley
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.
 

firewood

macrumors G3
Jul 29, 2003
8,106
1,343
Silicon Valley
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

macrumors regular
Nov 19, 2010
226
0
Nashville, TN
From the video.

Code:
-(IBAction)pushButton6 {
	NSString *path =[[NSBundle mainBundle]
					 pathForResource:@"Aah" ofType:@"wav"];
        [B]if (theAudio)[theAudio release];[/B]
	AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
	theAudio.delegate = self;
	[theAudio play];
	
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.