Sound stop playing [duplicate] - iphone

When I press a button, then press another one, the sounds overlap. How can I fix that so the first sound stops when another one is pressed?
- (void)playOnce:(NSString *)aSound {
NSString *path = [[NSBundle mainBundle] pathForResource:aSound ofType:#"caf"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[theAudio setDelegate: self];
[theAudio setNumberOfLoops:0];
[theAudio setVolume:1.0];
[theAudio play];
}
- (void)playLooped:(NSString *)aSound {
NSString *path = [[NSBundle mainBundle] pathForResource:aSound ofType:#"caf"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[theAudio setDelegate: self];
// loop indefinitely
[theAudio setNumberOfLoops:-1];
[theAudio setVolume:1.0];
[theAudio play];
[theAudio release];
}

Declare your AVAudioPlayer in the header the viewController ( don't alloc a new one each time you play a sound). That way you will have a pointer you can use in a StopAudio method.
#interface myViewController : UIViewController <AVAudioPlayerDelegate> {
AVAudioPlayer *theAudio;
}
#property (nonatomic, retain) AVAudioPlayer *theAudio;
#end
#implementation myViewController
#synthesize theAudio;
- (void)dealloc {
[theAudio release];
}
#end
- (void)playOnce:(NSString *)aSound {
NSString *path = [[NSBundle mainBundle] pathForResource:aSound ofType:#"caf"];
if(!theAudio){
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath:path] error:NULL];
}
[theAudio setDelegate: self];
[theAudio setNumberOfLoops:0];
[theAudio setVolume:1.0];
[theAudio play];
}
- (void)playLooped:(NSString *)aSound {
NSString *path = [[NSBundle mainBundle] pathForResource:aSound ofType:#"caf"];
if(!theAudio){
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath:path] error:NULL];
}
[theAudio setDelegate: self];
// loop indefinitely
[theAudio setNumberOfLoops:-1];
[theAudio setVolume:1.0];
[theAudio play];
}
- (void)stopAudio {
[theAudio stop];
[theAudio setCurrentTime:0];
}
also be sure to read the Apple Docs

The code you've posted will only play one sound, the first sound sent to the method.
If you want to only play one changeable sound, after stoping the player, release theAudio and then set the new sound.

Add a check to see if the player is already playing at the beginning of each method:
if (theAudio.playing == YES) {
[theAudio stop];
}
AVAudioPlayer Class Reference

In your playOnce method the 'path' variable is unused -- remove that to get rid of your warning message. Your playOnce does not set anything up to play so I'm not sure how that is supposed to work -- unless you call playLooped first? You also need to be calling prepareToPlay after the initWithContentsOfUrl.
- (void)playOnce:(NSString *)aSound {
NSString *path = [[NSBundle mainBundle] pathForResource:aSound ofType:#"caf"];
if (theAudio && [theAudio isPlaying]) {
[theAudio stop]
} else {
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath: path] error: NULL];
[theAudio prepareToPlay];
[theAudio setDelegate: self];
[theAudio setNumberOfLoops: 1];
[theAudio setVolume: 1.0];
[theAudio play];
}
}

You will need to utizilize a BOOL value to get this to work properly.
in your .m file BEFORE #implementation put this:
static BOOL soundIsPlaying = NO;
Then your IBAction needs to look something like this:
- (IBAction)play {
if (soundIsPlaying == YES) {
[theAudio release];
soundIsPlaying = NO;
}
else if (soundIsPlaying == NO) {
NSString *path = [[NSBundle mainBundle] pathForResource:#"SOUNDFILENAME" ofType:#"wav"];
theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
theAudio.volume = 1.0;
theAudio.numberOfLoops = 0;
[theAudio play];
soundIsPlaying = YES;
}
}
thats honestly about it. It will stop sounds when another button is pressed.

You can probably do something like the following:
(void) playLooped: (NSString * ) aSound {
NSString * path = [[NSBundle mainBundle] pathForResource: aSound ofType: #"caf"];
//stop audio player from playing so the sound don't overlap
if([theAudio isPlaying])
{
[theAudio stop]; //try this instead of stopAudio
}
if (!theAudio) {
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath: path] error: NULL];
}
[theAudio setDelegate: self];
// loop indefinitely
[theAudio setNumberOfLoops: -1];
[theAudio setVolume: 1.0];
[theAudio play];
}

Related

An AVPlayerItem cannot be associated with more than one instance of AVPlayer

I am getting an error when using AVAudioPlayer in Iphone. i got this error sometime not every time. error is :
An AVPlayerItem cannot be associated with more than one instance of AVPlayer
Code which i am using for playing a file :
NSString *soundPath =[[NSBundle mainBundle] pathForResource:#"abc" ofType:#"m4a"];
NSURL *soundURL = [NSURL fileURLWithPath:soundPath];
NSError *error;
AVAudioPlayer *theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error];
[theAudio setDelegate:self];
[theAudio setNumberOfLoops:0];
if(theAudio == nil)
{
NSLog([error description]);
}
else
{
[theAudio play];
}
can anyone tell me how can i solve this issue.

How do I stop the button from overlapping? [duplicate]

When I press a button, then press another one, the sounds overlap. How can I fix that so the first sound stops when another one is pressed?
- (void)playOnce:(NSString *)aSound {
NSString *path = [[NSBundle mainBundle] pathForResource:aSound ofType:#"caf"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[theAudio setDelegate: self];
[theAudio setNumberOfLoops:0];
[theAudio setVolume:1.0];
[theAudio play];
}
- (void)playLooped:(NSString *)aSound {
NSString *path = [[NSBundle mainBundle] pathForResource:aSound ofType:#"caf"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[theAudio setDelegate: self];
// loop indefinitely
[theAudio setNumberOfLoops:-1];
[theAudio setVolume:1.0];
[theAudio play];
[theAudio release];
}
Declare your AVAudioPlayer in the header the viewController ( don't alloc a new one each time you play a sound). That way you will have a pointer you can use in a StopAudio method.
#interface myViewController : UIViewController <AVAudioPlayerDelegate> {
AVAudioPlayer *theAudio;
}
#property (nonatomic, retain) AVAudioPlayer *theAudio;
#end
#implementation myViewController
#synthesize theAudio;
- (void)dealloc {
[theAudio release];
}
#end
- (void)playOnce:(NSString *)aSound {
NSString *path = [[NSBundle mainBundle] pathForResource:aSound ofType:#"caf"];
if(!theAudio){
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath:path] error:NULL];
}
[theAudio setDelegate: self];
[theAudio setNumberOfLoops:0];
[theAudio setVolume:1.0];
[theAudio play];
}
- (void)playLooped:(NSString *)aSound {
NSString *path = [[NSBundle mainBundle] pathForResource:aSound ofType:#"caf"];
if(!theAudio){
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath:path] error:NULL];
}
[theAudio setDelegate: self];
// loop indefinitely
[theAudio setNumberOfLoops:-1];
[theAudio setVolume:1.0];
[theAudio play];
}
- (void)stopAudio {
[theAudio stop];
[theAudio setCurrentTime:0];
}
also be sure to read the Apple Docs
The code you've posted will only play one sound, the first sound sent to the method.
If you want to only play one changeable sound, after stoping the player, release theAudio and then set the new sound.
Add a check to see if the player is already playing at the beginning of each method:
if (theAudio.playing == YES) {
[theAudio stop];
}
AVAudioPlayer Class Reference
In your playOnce method the 'path' variable is unused -- remove that to get rid of your warning message. Your playOnce does not set anything up to play so I'm not sure how that is supposed to work -- unless you call playLooped first? You also need to be calling prepareToPlay after the initWithContentsOfUrl.
- (void)playOnce:(NSString *)aSound {
NSString *path = [[NSBundle mainBundle] pathForResource:aSound ofType:#"caf"];
if (theAudio && [theAudio isPlaying]) {
[theAudio stop]
} else {
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath: path] error: NULL];
[theAudio prepareToPlay];
[theAudio setDelegate: self];
[theAudio setNumberOfLoops: 1];
[theAudio setVolume: 1.0];
[theAudio play];
}
}
You will need to utizilize a BOOL value to get this to work properly.
in your .m file BEFORE #implementation put this:
static BOOL soundIsPlaying = NO;
Then your IBAction needs to look something like this:
- (IBAction)play {
if (soundIsPlaying == YES) {
[theAudio release];
soundIsPlaying = NO;
}
else if (soundIsPlaying == NO) {
NSString *path = [[NSBundle mainBundle] pathForResource:#"SOUNDFILENAME" ofType:#"wav"];
theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
theAudio.volume = 1.0;
theAudio.numberOfLoops = 0;
[theAudio play];
soundIsPlaying = YES;
}
}
thats honestly about it. It will stop sounds when another button is pressed.
You can probably do something like the following:
(void) playLooped: (NSString * ) aSound {
NSString * path = [[NSBundle mainBundle] pathForResource: aSound ofType: #"caf"];
//stop audio player from playing so the sound don't overlap
if([theAudio isPlaying])
{
[theAudio stop]; //try this instead of stopAudio
}
if (!theAudio) {
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath: path] error: NULL];
}
[theAudio setDelegate: self];
// loop indefinitely
[theAudio setNumberOfLoops: -1];
[theAudio setVolume: 1.0];
[theAudio play];
}

iPhone sound not stopping when a second sound is played [duplicate]

When I press a button, then press another one, the sounds overlap. How can I fix that so the first sound stops when another one is pressed?
- (void)playOnce:(NSString *)aSound {
NSString *path = [[NSBundle mainBundle] pathForResource:aSound ofType:#"caf"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[theAudio setDelegate: self];
[theAudio setNumberOfLoops:0];
[theAudio setVolume:1.0];
[theAudio play];
}
- (void)playLooped:(NSString *)aSound {
NSString *path = [[NSBundle mainBundle] pathForResource:aSound ofType:#"caf"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[theAudio setDelegate: self];
// loop indefinitely
[theAudio setNumberOfLoops:-1];
[theAudio setVolume:1.0];
[theAudio play];
[theAudio release];
}
Declare your AVAudioPlayer in the header the viewController ( don't alloc a new one each time you play a sound). That way you will have a pointer you can use in a StopAudio method.
#interface myViewController : UIViewController <AVAudioPlayerDelegate> {
AVAudioPlayer *theAudio;
}
#property (nonatomic, retain) AVAudioPlayer *theAudio;
#end
#implementation myViewController
#synthesize theAudio;
- (void)dealloc {
[theAudio release];
}
#end
- (void)playOnce:(NSString *)aSound {
NSString *path = [[NSBundle mainBundle] pathForResource:aSound ofType:#"caf"];
if(!theAudio){
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath:path] error:NULL];
}
[theAudio setDelegate: self];
[theAudio setNumberOfLoops:0];
[theAudio setVolume:1.0];
[theAudio play];
}
- (void)playLooped:(NSString *)aSound {
NSString *path = [[NSBundle mainBundle] pathForResource:aSound ofType:#"caf"];
if(!theAudio){
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath:path] error:NULL];
}
[theAudio setDelegate: self];
// loop indefinitely
[theAudio setNumberOfLoops:-1];
[theAudio setVolume:1.0];
[theAudio play];
}
- (void)stopAudio {
[theAudio stop];
[theAudio setCurrentTime:0];
}
also be sure to read the Apple Docs
The code you've posted will only play one sound, the first sound sent to the method.
If you want to only play one changeable sound, after stoping the player, release theAudio and then set the new sound.
Add a check to see if the player is already playing at the beginning of each method:
if (theAudio.playing == YES) {
[theAudio stop];
}
AVAudioPlayer Class Reference
In your playOnce method the 'path' variable is unused -- remove that to get rid of your warning message. Your playOnce does not set anything up to play so I'm not sure how that is supposed to work -- unless you call playLooped first? You also need to be calling prepareToPlay after the initWithContentsOfUrl.
- (void)playOnce:(NSString *)aSound {
NSString *path = [[NSBundle mainBundle] pathForResource:aSound ofType:#"caf"];
if (theAudio && [theAudio isPlaying]) {
[theAudio stop]
} else {
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath: path] error: NULL];
[theAudio prepareToPlay];
[theAudio setDelegate: self];
[theAudio setNumberOfLoops: 1];
[theAudio setVolume: 1.0];
[theAudio play];
}
}
You will need to utizilize a BOOL value to get this to work properly.
in your .m file BEFORE #implementation put this:
static BOOL soundIsPlaying = NO;
Then your IBAction needs to look something like this:
- (IBAction)play {
if (soundIsPlaying == YES) {
[theAudio release];
soundIsPlaying = NO;
}
else if (soundIsPlaying == NO) {
NSString *path = [[NSBundle mainBundle] pathForResource:#"SOUNDFILENAME" ofType:#"wav"];
theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
theAudio.volume = 1.0;
theAudio.numberOfLoops = 0;
[theAudio play];
soundIsPlaying = YES;
}
}
thats honestly about it. It will stop sounds when another button is pressed.
You can probably do something like the following:
(void) playLooped: (NSString * ) aSound {
NSString * path = [[NSBundle mainBundle] pathForResource: aSound ofType: #"caf"];
//stop audio player from playing so the sound don't overlap
if([theAudio isPlaying])
{
[theAudio stop]; //try this instead of stopAudio
}
if (!theAudio) {
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath: path] error: NULL];
}
[theAudio setDelegate: self];
// loop indefinitely
[theAudio setNumberOfLoops: -1];
[theAudio setVolume: 1.0];
[theAudio play];
}

Sound overlapping with multiple button presses

When I press a button, then press another one, the sounds overlap. How can I fix that so the first sound stops when another one is pressed?
- (void)playOnce:(NSString *)aSound {
NSString *path = [[NSBundle mainBundle] pathForResource:aSound ofType:#"caf"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[theAudio setDelegate: self];
[theAudio setNumberOfLoops:0];
[theAudio setVolume:1.0];
[theAudio play];
}
- (void)playLooped:(NSString *)aSound {
NSString *path = [[NSBundle mainBundle] pathForResource:aSound ofType:#"caf"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[theAudio setDelegate: self];
// loop indefinitely
[theAudio setNumberOfLoops:-1];
[theAudio setVolume:1.0];
[theAudio play];
[theAudio release];
}
Declare your AVAudioPlayer in the header the viewController ( don't alloc a new one each time you play a sound). That way you will have a pointer you can use in a StopAudio method.
#interface myViewController : UIViewController <AVAudioPlayerDelegate> {
AVAudioPlayer *theAudio;
}
#property (nonatomic, retain) AVAudioPlayer *theAudio;
#end
#implementation myViewController
#synthesize theAudio;
- (void)dealloc {
[theAudio release];
}
#end
- (void)playOnce:(NSString *)aSound {
NSString *path = [[NSBundle mainBundle] pathForResource:aSound ofType:#"caf"];
if(!theAudio){
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath:path] error:NULL];
}
[theAudio setDelegate: self];
[theAudio setNumberOfLoops:0];
[theAudio setVolume:1.0];
[theAudio play];
}
- (void)playLooped:(NSString *)aSound {
NSString *path = [[NSBundle mainBundle] pathForResource:aSound ofType:#"caf"];
if(!theAudio){
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath:path] error:NULL];
}
[theAudio setDelegate: self];
// loop indefinitely
[theAudio setNumberOfLoops:-1];
[theAudio setVolume:1.0];
[theAudio play];
}
- (void)stopAudio {
[theAudio stop];
[theAudio setCurrentTime:0];
}
also be sure to read the Apple Docs
The code you've posted will only play one sound, the first sound sent to the method.
If you want to only play one changeable sound, after stoping the player, release theAudio and then set the new sound.
Add a check to see if the player is already playing at the beginning of each method:
if (theAudio.playing == YES) {
[theAudio stop];
}
AVAudioPlayer Class Reference
In your playOnce method the 'path' variable is unused -- remove that to get rid of your warning message. Your playOnce does not set anything up to play so I'm not sure how that is supposed to work -- unless you call playLooped first? You also need to be calling prepareToPlay after the initWithContentsOfUrl.
- (void)playOnce:(NSString *)aSound {
NSString *path = [[NSBundle mainBundle] pathForResource:aSound ofType:#"caf"];
if (theAudio && [theAudio isPlaying]) {
[theAudio stop]
} else {
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath: path] error: NULL];
[theAudio prepareToPlay];
[theAudio setDelegate: self];
[theAudio setNumberOfLoops: 1];
[theAudio setVolume: 1.0];
[theAudio play];
}
}
You will need to utizilize a BOOL value to get this to work properly.
in your .m file BEFORE #implementation put this:
static BOOL soundIsPlaying = NO;
Then your IBAction needs to look something like this:
- (IBAction)play {
if (soundIsPlaying == YES) {
[theAudio release];
soundIsPlaying = NO;
}
else if (soundIsPlaying == NO) {
NSString *path = [[NSBundle mainBundle] pathForResource:#"SOUNDFILENAME" ofType:#"wav"];
theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
theAudio.volume = 1.0;
theAudio.numberOfLoops = 0;
[theAudio play];
soundIsPlaying = YES;
}
}
thats honestly about it. It will stop sounds when another button is pressed.
You can probably do something like the following:
(void) playLooped: (NSString * ) aSound {
NSString * path = [[NSBundle mainBundle] pathForResource: aSound ofType: #"caf"];
//stop audio player from playing so the sound don't overlap
if([theAudio isPlaying])
{
[theAudio stop]; //try this instead of stopAudio
}
if (!theAudio) {
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath: path] error: NULL];
}
[theAudio setDelegate: self];
// loop indefinitely
[theAudio setNumberOfLoops: -1];
[theAudio setVolume: 1.0];
[theAudio play];
}

sound not playing when i press the button and how to fix overlapping sounds [duplicate]

When I press a button, then press another one, the sounds overlap. How can I fix that so the first sound stops when another one is pressed?
- (void)playOnce:(NSString *)aSound {
NSString *path = [[NSBundle mainBundle] pathForResource:aSound ofType:#"caf"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[theAudio setDelegate: self];
[theAudio setNumberOfLoops:0];
[theAudio setVolume:1.0];
[theAudio play];
}
- (void)playLooped:(NSString *)aSound {
NSString *path = [[NSBundle mainBundle] pathForResource:aSound ofType:#"caf"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[theAudio setDelegate: self];
// loop indefinitely
[theAudio setNumberOfLoops:-1];
[theAudio setVolume:1.0];
[theAudio play];
[theAudio release];
}
Declare your AVAudioPlayer in the header the viewController ( don't alloc a new one each time you play a sound). That way you will have a pointer you can use in a StopAudio method.
#interface myViewController : UIViewController <AVAudioPlayerDelegate> {
AVAudioPlayer *theAudio;
}
#property (nonatomic, retain) AVAudioPlayer *theAudio;
#end
#implementation myViewController
#synthesize theAudio;
- (void)dealloc {
[theAudio release];
}
#end
- (void)playOnce:(NSString *)aSound {
NSString *path = [[NSBundle mainBundle] pathForResource:aSound ofType:#"caf"];
if(!theAudio){
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath:path] error:NULL];
}
[theAudio setDelegate: self];
[theAudio setNumberOfLoops:0];
[theAudio setVolume:1.0];
[theAudio play];
}
- (void)playLooped:(NSString *)aSound {
NSString *path = [[NSBundle mainBundle] pathForResource:aSound ofType:#"caf"];
if(!theAudio){
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath:path] error:NULL];
}
[theAudio setDelegate: self];
// loop indefinitely
[theAudio setNumberOfLoops:-1];
[theAudio setVolume:1.0];
[theAudio play];
}
- (void)stopAudio {
[theAudio stop];
[theAudio setCurrentTime:0];
}
also be sure to read the Apple Docs
The code you've posted will only play one sound, the first sound sent to the method.
If you want to only play one changeable sound, after stoping the player, release theAudio and then set the new sound.
Add a check to see if the player is already playing at the beginning of each method:
if (theAudio.playing == YES) {
[theAudio stop];
}
AVAudioPlayer Class Reference
In your playOnce method the 'path' variable is unused -- remove that to get rid of your warning message. Your playOnce does not set anything up to play so I'm not sure how that is supposed to work -- unless you call playLooped first? You also need to be calling prepareToPlay after the initWithContentsOfUrl.
- (void)playOnce:(NSString *)aSound {
NSString *path = [[NSBundle mainBundle] pathForResource:aSound ofType:#"caf"];
if (theAudio && [theAudio isPlaying]) {
[theAudio stop]
} else {
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath: path] error: NULL];
[theAudio prepareToPlay];
[theAudio setDelegate: self];
[theAudio setNumberOfLoops: 1];
[theAudio setVolume: 1.0];
[theAudio play];
}
}
You will need to utizilize a BOOL value to get this to work properly.
in your .m file BEFORE #implementation put this:
static BOOL soundIsPlaying = NO;
Then your IBAction needs to look something like this:
- (IBAction)play {
if (soundIsPlaying == YES) {
[theAudio release];
soundIsPlaying = NO;
}
else if (soundIsPlaying == NO) {
NSString *path = [[NSBundle mainBundle] pathForResource:#"SOUNDFILENAME" ofType:#"wav"];
theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
theAudio.volume = 1.0;
theAudio.numberOfLoops = 0;
[theAudio play];
soundIsPlaying = YES;
}
}
thats honestly about it. It will stop sounds when another button is pressed.
You can probably do something like the following:
(void) playLooped: (NSString * ) aSound {
NSString * path = [[NSBundle mainBundle] pathForResource: aSound ofType: #"caf"];
//stop audio player from playing so the sound don't overlap
if([theAudio isPlaying])
{
[theAudio stop]; //try this instead of stopAudio
}
if (!theAudio) {
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath: path] error: NULL];
}
[theAudio setDelegate: self];
// loop indefinitely
[theAudio setNumberOfLoops: -1];
[theAudio setVolume: 1.0];
[theAudio play];
}