I am using AVAudioPLayer to do the audio for my game. I have the following code in a method in a seperate class, which is called every time I want the 'boing' sound to play. Problem is, it leaks huge amounts of memory, to the point where the game becomes unplayable. Now i'm not releasing any of the pointers in the method, because at the time I only had one. But now I have 10 of these methods.
What is the best way to release the pointers in tis case? (Yes, I have tried releasing straight after [boing play];, this solves the leak (obviously) but the sound dosen't play so theres no point.
-(void)playBoing {
int x = (arc4random()%3)+1;
NSString *path = [NSString stringWithFormat:#"/boing_0%i.aif", x];
NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
resourcePath = [resourcePath stringByAppendingString:path];
AVAudioPlayer *boing = [[AVAudioPlayer alloc] initWithContentsOfURL:
[NSURL fileURLWithPath:resourcePath] error:nil];
boing.delegate = self;
boing.volume = 1;
[boing play];
}
It's possible there's a better solution, but I've had similar problems where releasing right away killed my process. This is the solution I took. Again, there may be better out there, but for a quick fix, it should do the trick.
In your header file, create the following:
AVAudioPlayer *boing;
Then in -(void)playBoing, do as you did, but change
AVAudioPlayer *boing = [[AVAudioPlayer alloc] initWithContentsOfURL:
[NSURL fileURLWithPath:resourcePath] error:nil];
to
if (boing != nil) {
boing = nil;
[boing release];
}
boing = [[AVAudioPlayer alloc] initWithContentsOfURL:
[NSURL fileURLWithPath:resourcePath] error:nil];
This should ensure that there's only one instance of boing allocated at a time
Related
I searched everywhere but I couldn't find an answer for my problem.
I play multiple sounds on my app and I let the user to adjust the volume with a button. So if the user wants, he can play with .5 volume. So I have this code:
NSString *path = [[NSBundle mainBundle] pathForResource:#"Orchestra" ofType:#"mp3"];
theOrchestras = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath: path] error: NULL];
[theOrchestras play];
and to reduce the volume this:
- (IBAction) volumeReduced {
theOrchestras.volume = 0.5;
}
When the sounds is playing the volume is reduced, no problem. But when the sounds stops, and plays again, the volume always goes back to 1.
To fix this I tried to save the volume as an Integer, but for some reason the value is always 0, I couldn't make it work:
.m
int64_t * volumeSaved;
.h
- (IBAction) volumeReduced {
volumeSaved = .5;
}
NSString *path = [[NSBundle mainBundle] pathForResource:#"Orchestra" ofType:#"mp3"];
theOrchestras = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath: path] error: NULL];
theOrchestras.volume = volumeSaved;
[theOrchestras play];
But the audio now always plays with 0 volume, muted.
Is there any way to do this with a button?
Thanks!
You're declaring volumeSaved as a pointer to an integer instead of an integer, but then assigning to it like it's an integer. That's one oddity.
But your real problem is that the volume property in AVAudioPlayer is a float, not a int64_t. So change the type to:
float volumeSaved;
Try storing the volume with NSNumber.
I am trying to loop my sound file and I cannot find the parameter to loop the sound file! I'm about to pull out the last standing hair on my head. It loads fine....it plays....I just can't figure out the code on how to make it loop.
Here is my code:
NSString *eyeBGPath = [[NSBundle mainBundle] pathForResource:#"theeye" ofType:#"caf"];
CFURLRef eyeBGURL = (CFURLRef ) [NSURL fileURLWithPath:eyeBGPath];
AudioServicesCreateSystemSoundID(eyeBGURL, &eyeBackGroundID);
AudioServicesPlaySystemSound(eyeBackGroundID);
What am I doing wrong here?
I found this:
loops
Indicates whether the receiver restarts playback when it reaches the end of its content. Default: NO.
- (BOOL)loops
Return Value
YES when the receiver restarts playback when it finishes, NO otherwise.
Availability
Available in Mac OS X v10.5 and later.
See Also
– setLoops:
Declared In
NSSound.h
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSSound_Class/Reference/Reference.html%23//apple_ref/occ/instm/NSSound/loops
I'm confused on how to implement this.
This is the code that worked for me. If anyone needs help with this, let me know. Someone on the Apple Dev forum pointed me to the AVFoundation docs.
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/theeye3.caf", [[NSBundle mainBundle] resourcePath]]];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
audioPlayer.numberOfLoops = -1;
[audioPlayer play];
Here is code to pick out a sound file based on a random number from an image array.
NSString *audioWiz = [[NSString alloc]initWithFormat:#"%d.caf", randNum];
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:[#"%#/" stringByAppendingString:audioWiz], [[NSBundle mainBundle] resourcePath]]];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
audioPlayer.numberOfLoops = 0;
audioPlayer.volume = 0.1;
[audioPlayer play];
I think the answer you are looking for is here.
Short answer, use a MPMusicPlayerController.
What is the simplest way to play a looping sound in an iPhone app?
Probably the easiest solution would be to use an AVAudioPlayer with the numberOfLoops: set to a negative integer.
// *** In your interface... ***
#import <AVFoundation/AVFoundation.h>
...
AVAudioPlayer *testAudioPlayer;
// *** Implementation... ***
// Load the audio data
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"sample_name" ofType:#"wav"];
NSData *sampleData = [[NSData alloc] initWithContentsOfFile:soundFilePath];
NSError *audioError = nil;
// Set up the audio player
testAudioPlayer = [[AVAudioPlayer alloc] initWithData:sampleData error:&audioError];
[sampleData release];
if(audioError != nil) {
NSLog(#"An audio error occurred: \"%#\"", audioError);
}
else {
[testAudioPlayer setNumberOfLoops: -1];
[testAudioPlayer play];
}
// *** In your dealloc... ***
[testAudioPlayer release];
You should also remember to set the appropriate audio category. (See the AVAudioSession setCategory:error: method.)
Finally, you'll need to add the AVFoundation library to your project. To do this, alt click on your project's target in the Groups & Files column in Xcode, and select "Get Info". Then select the General tab, click the + in the bottom "Linked Libraries" pane and select "AVFoundation.framework".
The easiest way would be to use an AVAudioPlayer set to an infinite number of loops (or finite if that's what you need).
Something like:
NSString *path = [[NSBundle mainBundle] pathForResource:#"yourAudioFileName" ofType:#"mp3"];
NSURL *file = [[NSURL alloc] initFileURLWithPath:path];
AVAudioPlayer *_player = [[AVAudioPlayer alloc] initWithContentsOfURL:file error:nil];
[file release];
_player.numberOfLoops = -1;
[_player prepareToPlay];
[_player play];
This will simply loop whatever audio file you have specified indefinitely.
Set the number of loops to any positive integer if you have a finite number of times you want the audio file to loop.
Hope this helps.
Cheers.
I want to play an audio file which is posted on a website. I'm trying to use AVAudioPlayer and set the url to its website link. But the bgPlayer turns out to be nil. I think I can download this audio file and play it afterwards, but is there any way to play it while I'm downloading it?
NSURL *bgURL = [NSURL URLWithString:#"http://www.radioslots.com/iphone/test/tmp/1.mp3"];
bgPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:bgURL error:nil];
bgPlayer.numberOfLoops = -1;
if (bgPlayer) {
[bgPlayer play];
}
AVAudioPlayer isn't made to open network streams, I could be mistaken but I'm pretty sure it's not possible.
Just off the top of my head, I can think of two ways that would probably work:
1. Use MPMoviePlayer, as this can open network streams and despite the name works with audio. The only thing is this pops up the modal player with controls.
2. Download the network file and store it locally, then use AVAudioPlayer to play the local file.
I have just written answer here.Check it and replace the url used there with yours..
you just replace
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"songname" ofType:#"mp3"];
NSURL *newURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
with
NSString *soundFilePath = #"Your URL";
NSURL *newURL = [[NSURL alloc] initWithString: soundFilePath];
There is a proper documentation available at this link
But I think you should also check the error code you are getting by this way:
NSError *error;
bgPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:bgURL error:&error];
Hope this helps.
Considering the code:
soundFilePath = [[NSBundle mainBundle] pathForResource: #"Sound" ofType: #"wav"];
fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
avPlayerNextLevel = [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: nil];
avPlayerNextLevel1.volume = volume ;
[soundFilePath release];
[fileURL release];
To play the sound I then do
if([avPlayerNextLevel prepareToPlay]){
[avPlayerNextLevel play];
}
I do this many times within my game. Sometimes on the simulator , the sound stops playing. And when sometimes I detect memory leaks with AvAudioPlayer.
Does everything look alright with my code ?
You'll need to release the instance of AVAudioPlayer that you created. Since the above code sets the delegate to self, just implement the following method and release there:
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
NSLog(#"Audio finished playing.");
[player release];
}
Why are you releaseing a non-owning reference (soundFilePath)?
You don't need to manually prepareToPlay if you're going to invoke play right after.
Quoting the documentation for play:
Discussion
Calling this method implicitly calls the prepareToPlay method if the audio player is not already prepared to play.
Personally, I do this:
NSURL *soundUrl = [NSURL fileURLWithPath:
[[NSBundle mainBundle] pathForResource:track
ofType:nil]];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl
error:nil];
[audioPlayer setDelegate:self];
[audioPlayer play];
Where audioPlayer is an ivar and track is an lvar of type NSString* which I obtained earlier from wherever I configure the next song to play.
I've been working on something similar but I was using a slightly different piece of code. Unfortunately, I got memory leaks, therefore I decided to give a try to the code you posted. Same result: memory leaks again when I invoke [audioPlayer play];
I can't really figure out what's going on. Are you guys aware of any memory leaks when AVAudioPlayer is involved?