PDA

View Full Version : AVAudioPlayer Help




PanzyCat
Jul 16, 2010, 10:36 AM
Hey guys

I was wondering if you could help me with my issue below.



- (IBAction)playsound1 {

NSString *path = [[NSBundle mainBundle] pathForResource:@"song" ofType:@"wav"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: path] error:NULL];
[theAudio play];

}

When I press the buttons often, after 200-250 times pressing the buttons, the buttons don't sound anymore. I can continue pressing the buttons, but no sound appears. But the application doesn't break down, i can continue using my application fine, but only the sound doesn't sound anymore, so I have to restart the application and after the restart everything works fine again, till I pressed the sound-buttons 200-250 times and the game begins again. I can't figure out the problem.

If you could help, that would be great.



robbieduncan
Jul 16, 2010, 11:09 AM
Do you ever release theAudio? Because you should as as per the Cocoa Memory Management Rules (http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html#//apple_ref/doc/uid/20000994-BAJHFBGH) you "own" that object. I assume you are familiar with the entirety of the document I linked to?

PanzyCat
Jul 16, 2010, 11:24 AM
No, sorry. I actually downloaded xcode yesterday, and watched a tutorial on how to make sounds. I wanted to make a little soundboard.

I know that I would release it in the .h file, but I don't know how I would do that?

robbieduncan
Jul 16, 2010, 11:25 AM
I know that I would release it in the .h file

No, you absolutely wouldn't do it in the .h file. I suggest you read the document I linked to.

PanzyCat
Jul 16, 2010, 11:30 AM
From what I read in the link you just sent me, this is what I believe it should look like.

- (IBAction)playsound1 {

NSString *path = [[NSBundle mainBundle] pathForResource:@"song" ofType:@"wav"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: path] error:NULL];
[theAudio play];
[theAudio release];

}

robbieduncan
Jul 16, 2010, 11:31 AM
From what I read in the link you just sent me, this is what I believe it should look like.

- (IBAction)playsound1 {

NSString *path = [[NSBundle mainBundle] pathForResource:@"song" ofType:@"wav"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: path] error:NULL];
[theAudio play];
[theAudio release];

}

I suggest that that will probably not do what you want. You want to release the theAudio when the sound finishes playing. Look at the delegate methods.

PanzyCat
Jul 16, 2010, 11:48 AM
I suggest that that will probably not do what you want. You want to release the theAudio when the sound finishes playing. Look at the delegate methods.


Is this in the link you sent me?

I get that I have to release theAudio after 1 count. I don't know how to tell it to do that.

PanzyCat
Jul 16, 2010, 02:19 PM
NSString *path = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"wav"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];

if([theAudio initWithData:audioData error:NULL]) {
[theAudio autorelease];
} else {
[theAudio release];
theAudio = nil;
}
return theAudio;
}


is that right?