views:

98

answers:

1

I am developing an application that involves clicking a button repeatedly, and I am giving an audio feedback for this click, a click wave file.

it works fine, but I have a problem, after exactly 248 times, the sound is not played any more, the app doesn't crash however. and I get the following error:

Error Domain=NSOSStatusErrorDomain Code=-43 "The operation couldn't be completed. (NSOSStatusErrorDomain error -43.)"

here is the code I am using, (pretty standard)

//file.h
AVAudioPlayer *audioPlayer;


//file.m
    NSString *path=[[NSBundle mainBundle] pathForResource:@"click" ofType:@"wav"];

        [audioPlayer stop]; //I added this in one of my trials to solve it.
        audioPlayer= [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path]  error:&err];
        audioPlayer.numberOfLoops=0;
        audioPlayer.delegate=self;
           [audioPlayer prepareToPlay];



        if (audioPlayer==nil)
            NSLog(@"%@, %@",path,[err description]);
        else
        [audioPlayer play];

any idea why this happens?

+2  A: 

I've only ever seen that error message associated with streaming audio. Might be something strange happening with the NSURL or...

Other issue with your code: When do you release the audioPlayer object? If you aren't, you're allocating a lot of objects and never releasing them from memory. That could eventually lead to a low memory message.

Another issue: Apple recommends using an IMA4 encoded *.caf file for playing multiple sounds at once

Lastly, I have posted a sample project from another question which might show a better way of using AVAudioPlayer ...See this post http://stackoverflow.com/questions/3128283/what-is-the-best-way-to-play-sound-quickly-upon-fast-button-presses-xcode/3129905#3129905

iWasRobbed
You are correct, I didn't release the audioplayer correctly, and I didn't notice that earlier, but after you pointed that out, I checked my code again, and I fixed it.now it works, thanks.
Tamer