SKAction playSoundFileNamed failed in 500 mp3

In my application I need to use a lot of short different mp3s (about 500 items one at a time)

So I use SKAction playSoundFileNamed

After ~ 200 sounds, it crashed with "Could not load resource - resource s234.mp3 cannot be loaded." Memory rises to 70 mb.

How to avoid this?

What I tried:

  • recreate sound at each iteration

    SKAction *mySound=[SKAction playSoundFileNamed:aa waitForCompletion:YES];
    
  • create one variable at the beginning of .m

    SKAction *mySound;
    

and reuse it in iterations

    mySound=[SKAction playSoundFileNamed:aa waitForCompletion:YES];

2. load all sounds into the array once at startup

for (int j=0;j<500;j++){
        NSString *aa=[NSString stringWithFormat:@"s%d.mp3", j];
        [item.sounds addObject:[SKAction playSoundFileNamed:aa waitForCompletion:YES]];
    }

... but never changed - it crashes and cannot download mp3.

How to clear a memory leak?

EDITED I also tried disabling ARC and manually disabling it every time. Nothing changed.

0
3

SKAction, .

https://github.com/pepelkod/iOS-Examples/tree/master/PlaySoundWithVolume

+(SKAction*)playSoundFileNamed:(NSString*)fileName atVolume:(CGFloat)volume waitForCompletion:(BOOL)wait{
// setup audio
NSString*   nameOnly = [fileName stringByDeletingPathExtension];
NSString*   extension = [fileName pathExtension];
NSURL *soundPath = [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:nameOnly ofType:extension]];
AVAudioPlayer *player = [[AVAudioPlayer alloc]initWithContentsOfURL:soundPath error:NULL];
[player setVolume:volume];
[player prepareToPlay];

SKAction*   playAction = [SKAction runBlock:^{
    [player play];
}];
if(wait == YES){
    SKAction*   waitAction = [SKAction waitForDuration:player.duration];
    SKAction* groupActions = [SKAction group:@[playAction, waitAction]];
    return groupActions;
}
return playAction;

}

+1

"" . AVAudioPlayer , ObjectAL ( Kobold Kit). , , (MP3) , ( CAF WAV).

, , 5 , MP3 . CAF/WAV .

, . , mp3 250 , 500, 120 . mp3 , , , .

+1

, . , , playSoundFileNamed , ... .

250 . , , . , ~ 200 , . , , Xcode , , png ( , ), ,

error = 24 ( )

, . , init .

I searched and searched for a way to close these files, but came up with nothing. I'm thinking of introducing a singleton to start my sound and move all the calls to playSoundFileNamed, so they only ever get called once. I think this is a mistake from Apple. These files must be closed using ARC. Has anyone found something like this?

-1
source

Source: https://habr.com/ru/post/1676579/


All Articles