Sound resource cannot be loaded Swift SKAction

I have an application that plays a sound file every time I touch the screen. For some reason, the application will crash every time with the following error:

reason: 'Resource tick.mp3 can not be loaded' 

If you need it, here is how I play the file every time I press the screen:

 runAction(SKAction.playSoundFileNamed("tick.mp3", waitForCompletion: false)) 

This does not happen very often, maybe 1 out of 10 application launches. In most cases, everything works as expected. I would like to know what I'm doing to cause a crash, but I have no clue! I just step back, it would seem, not otherwise than the times when he does not fall. Then I suddenly get this problem ...

+5
source share
2 answers

If you play sound using the playSound function, it will work

 var soundFile = SKAction.playSoundFileNamed("bark.wav", waitForCompletion: false) playSound(soundFile) 

PlaySound:

 func playSound(soundVariable : SKAction) { runAction(soundVariable) } 
+3
source

First of all, it looks like you are using an mp3 file to play (short) sound effects. When using mp3, the sound is compressed. In memory, it will have different sizes. There is also a penalty for decoding (decoding takes CPU time). Most importantly, and the reason I'm talking about mp3 files can be found in docs :

When using hardware decoding, a device can only play one instance of one of the supported formats at a time. For example, if you play MP3 stereo sound using hardware codec, the second simultaneous MP3 sound will use software decoding. Similarly, you cannot play AAC and ALAC audio simultaneously using hardware. If the iPod application plays AAC or MP3 sound in the background, he stated that the hardware codec; your application then plays AAC, ALAC, and MP3 audio using software decoding.

As you can see, the problem is that only one mp3 file at a time can be played using hardware. If you play more than one mp3 file at the same time, they will be decoded using software, and this is slow.

So, I would recommend using .wav or .caf files to play sound effects. mp3 would probably be good for background music.

About crash:

  • try using .wav or .caf files instead of .mp3
  • try to keep a strong link in SKAction and reuse it as suggested by Reece Kenney.
+4
source

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


All Articles