简体   繁体   中英

Error 24 (Too many open files) - iPhone

I recently implemented sounds into my game. Every time the user touches the screen, an arrow is shot, and a sound is played. I am using the following code to play the sound:

- (void)arrowShoot {
    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/arrow.mp3", [[NSBundle mainBundle] resourcePath]]];
    arrowShootPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    if (arrowShootPlayer != nil) {
        [arrowShootPlayer play];
    }
}

The reason I was allocating memory to the arrowShootPlayer every time is because the user can tap the screen rapidly, and I want the sounds to overlap. Otherwise, the sound only plays once the previous one has finished playing.

Anyway, I am getting this error:

<com.apple.main-thread> shm_open failed: "AppleAudioQueue.36.5755" (23) flags=0x2 errno=24

Once I get this error, the sounds stop being played. Everything else is fine, just the arrow sounds stop playing.

Is there a better way of playing 'overlapping' sounds like this on the iPhone, or is there a way around this error??

Thanks

if it is the same audio always, use the playing from memory buffer instead of opening the files. Declare some local buffer:

NSData* data;

then in your method:

if(!data) {
   data = [NSData dataWithContentsOfURL:url options:NSDataReadingMappedIfSafe error:&err];
}
arrowShootPlayer = [[AVAudioPlayer alloc] initWithData:data error:nil];
if (arrowShootPlayer) {
    [arrowShootPlayer play];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM