I am having trouble stopping a sound file from playing with a second touch of the same button. I am using the AVAudioPlayer frame work. I know that you must use the -(void)stop method. I am just not sure where to put that. Any assistance would be great.
I am having trouble stopping a sound file from playing with a second touch of the same button. I am using the AVAudioPlayer frame work. I know that you must use the -(void)stop method. I am just not sure where to put that. Any assistance would be great.
Is this something like what you're trying to accomplish?
I do my loading slightly differently, but it looks like the way that you're doing it should work. There's a syntax error on your first play. It should be:
Code:
[theAudio play];
You have a curly bracket instead of a square bracket at the beginning.
You'll have to define the method to be called when a button is pressed, in my example, the method is:
Code:
-(void) buttonPressed:(UIButton *)button
You'll also need to store the value for theAudio somewhere so that it's available to the buttonPressed method across different calls to the method. I was using myPlayer as an example of an instance variable in a viewController class that handles touches. Your application may well be quite different, but you'll need to store theAudio somewhere so that it's available to the buttonPressed method, assuming that theAudio is available, the buttonPressed method should be:
Im guessing I dont have to tell you that I am a fresh noob. Sorry Im not getting this quickly. Here is my .m file and my .h file. What am I doing wrong. Thank you very much for the help.
I do my loading slightly differently, but it looks like the way that you're doing it should work. There's a syntax error on your first play. It should be:
Code:
[theAudio play];
You have a curly bracket instead of a square bracket at the beginning.
You'll have to define the method to be called when a button is pressed, in my example, the method is:
Code:
-(void) buttonPressed:(UIButton *)button
You'll also need to store the value for theAudio somewhere so that it's available to the buttonPressed method across different calls to the method. I was using myPlayer as an example of an instance variable in a viewController class that handles touches. Your application may well be quite different, but you'll need to store theAudio somewhere so that it's available to the buttonPressed method, assuming that theAudio is available, the buttonPressed method should be:
Ahh, then you've already got myAction hooked up to a button in your view? If so, I'd do the code for more like this (Note that I'm not compiling this, so I might have some syntax errors, sorry...):
THE .h file
Code:
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <AVFoundation/AVAudioPlayer.h>
@interface MainView :UIView {
// the player
AVAudioPlayer* theAudio;
}
- (IBAction)myAction:(id)sender;
@end
The source file:
Code:
#import "MainView.h"
@implementation MainView
- (IBAction)myAction:(id)sender {
if (theAudio == nil) {
// Set up the player.
NSString *path = [[NSBundle mainBundle] pathForResource:@"boom" ofType:@"wav"];
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPathath] error:NULL];
theAudio.delegate = self;
[theAudio play];
}
else if ([theAudio isPlaying]) {
// there's no point in stopping it, if it's not playing...
[theAudio stop];
}
/* Uncomment this if you want the button to start the sound again...
else {
[theAudio play];
}
*/
}
@end
... at least I hope that this is close. To what you want. I didn't realize that you were using myAction as the target for the button. Also, note that this board seems to be automatically replacing colon open paren with a frowny face...
@interface MainView :UIView {
// the player
AVAudioPlayer* theAudio;
}
- (IBAction)myActionid)sender;
@end
And here it's saying that I havent declared theAudio. (First use in this Function. I had some other errors but I got those out of the way. These are the only two I couldnt figure out.
@implementation MainView
- (IBAction)myAction1id)sender {
if (theAudio == nil) {
Quote:
Originally Posted by deansx
Ahh, then you've already got myAction hooked up to a button in your view? If so, I'd do the code for more like this (Note that I'm not compiling this, so I might have some syntax errors, sorry...):
THE .h file
Code:
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <AVFoundation/AVAudioPlayer.h>
@interface MainView :UIView {
// the player
AVAudioPlayer* theAudio;
}
- (IBAction)myAction:(id)sender;
@end
The source file:
Code:
#import "MainView.h"
@implementation MainView
- (IBAction)myAction:(id)sender {
if (theAudio == nil) {
// Set up the player.
NSString *path = [[NSBundle mainBundle] pathForResource:@"boom" ofType:@"wav"];
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPathath] error:NULL];
theAudio.delegate = self;
[theAudio play];
}
else if ([theAudio isPlaying]) {
// there's no point in stopping it, if it's not playing...
[theAudio stop];
}
/* Uncomment this if you want the button to start the sound again...
else {
[theAudio play];
}
*/
}
@end
... at least I hope that this is close. To what you want. I didn't realize that you were using myAction as the target for the button. Also, note that this board seems to be automatically replacing colon open paren with a frowny face...
This is the implementation file. I am still getting these errors. I wrote the errors in the code exactly where and how they are appearing in xcode. Im guessing they are just little syntax errors.
#import "MainView.h"
EXPECTED SPECIFIER-QUALIFIER BEFORE AVAUDIOPLAYER
#import <AVFoundation/AVAudioPlayer.h>
From the code snippets, it's not obvious to me why theAudio isn't defined. Usually, when I see this it's because of either a typo somewhere in the header file, or else a typo in the name. I don't see anything obvious. It's probably unrelated, but I noticed that you appear to be wanting to use the <AVAudioPlayerDelegate>
protocol, but you're not adopting it in the class header.
So if I deleted the AvAudio framework and readded it to the project could that work? I have no idea why those syntax errors are showing up. I'm even more lost now.
This allowed me to declare theAudio in the header. I noticed that, in the fragment of source you copied above, you were still importing the file in the source.
I don't think that deleting the framework would help. The first thing that I'd check is making sure that the AVAudioPlayer.h is properly imported in your header.
If it would help, I can send you some source files that I know will work. I'm a bit hesitant to post them on the board, but I'd be happy to e-mail them to you. You could use them as a starting point.
Yea I just realized I posted my email address for everyone to see. Oh well. So I have heard that its a real pain to get anything into the app store. As far as building a distribution version of your app. Is that true?
So far, we haven't had any real problems, but I've also heard all of the stories, so we're pretty careful. We currently have two apps in the store, one more waiting approval and one update also waiting approval. The toughest part is the long wait, and just not knowing whether the app will be approved.
I haven't received them yet. Belive it or not my app isn't far from being ready to ship. The main thing in my way is getting sounds to stop on request. That and getting a feel for instruments and fixing leaks.