9

I'm seeing some small amounts of SpriteKit playSoundFileNamed crashes from my app's crash log. The crashes happen on iOS 8.3.

0 CoreFoundation __exceptionPreprocess  
1 libobjc.A.dylib objc_exception_throw
2 CoreFoundation -[NSException initWithCoder:]
3 SpriteKit +[SKPlaySound playSoundFileNamed:atPosition:waitForCompletion:]
4 SpriteKit +[SKAction(SKActions) playSoundFileNamed:waitForCompletion:]
...

And a few related crashes:

0 CoreFoundation __exceptionPreprocess  
1 libobjc.A.dylib objc_exception_throw
2 CoreFoundation -[NSException raise:format:]
3 SpriteKit +[SKPlaySound playSoundFileNamed:atPosition:waitForCompletion:]
4 SpriteKit +[SKAction(SKActions) playSoundFileNamed:waitForCompletion:]
...

Does anyone know what causes this crash and how to fix it? Should I wrap every calls to playSoundFileNamed: in a try-catch block?

Edited

More information:

I'm using Swift. Trying to play my own sounds and I'm seeing the crashes coming from different sounds. I'm also seeing a couple reports from iOS 8.2 so this crash might not be iOS 8.3 specific.

The lines that play the sound:

var sound = SKAction.playSoundFileNamed("Sound/ABC.mp3", waitForCompletion: false)
self.runAction(sound)
Ingo Dellwig
  • 226
  • 4
  • 22
user1615898
  • 1,185
  • 1
  • 10
  • 20
  • Please, provide more informations : Are you trying to play your own sound or one from the system bundle ? Is it a particular sounds that crashes or all of them ? Does it crash on iOS 8.3 only ? Can you paste the lines that create/play the sound ? etc... – lchamp Jun 19 '15 at 11:21
  • What language are you using - Swift or Obj-C ? – sangony Jun 19 '15 at 12:45
  • I'm using Swift. Trying to play my own sounds and I'm seeing the crashes coming from different sounds. I'm also seeing a couple reports from iOS 8.2 so it might now be iOS 8.3 specifically. – user1615898 Jun 20 '15 at 02:10
  • Have you tried with different audio encodings and/or different sound files? – aramusss Jun 22 '15 at 10:13
  • Hello where you able to find the answer? one of my apps crashes and throws the same exception while playing a sound. It doesn't happen all the time but only a few occasions – Araib karim Jul 07 '15 at 10:11
  • http://stackoverflow.com/questions/5720984/accelerometer-crashing-after-shaken-cocoa-touch maybe you should see this answer. – Araib karim Jul 07 '15 at 10:31

3 Answers3

2

I experienced a similar issue a while back. The issue was that the variable could not be made quickly enough to play as I was creating the variable each time the user tapped on the screen. Try defining the action in didMoveToView, and seeing if you still get the issue. Hope that helps

Justin Buergi
  • 131
  • 11
1

Try this and let me know if its working.

var audioPlayer = AVAudioPlayer()

func playAudio() {
    // Set the sound file name & extension
    let alertSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("ABC", ofType: "mp3")!)


    // Preperation
    try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, withOptions: [])
    try! AVAudioSession.sharedInstance().setActive(true)

    // Play the sound
    do {
        try audioPlayer = AVAudioPlayer(contentsOfURL: alertSound)
        audioPlayer.prepareToPlay()
        audioPlayer.play()
    } catch {
        print("there is \(error)")
    }
}
vard
  • 4,057
  • 2
  • 26
  • 46
omer15
  • 134
  • 12
0

I had similar problem. My game (swift + spritekit) was crashing indeterministic on iOS 8.x. but on 9.x works perfect. Piece of log:

2015-12-15 21:27:40.827 MyGame[24055:2285857]
        *** Terminating app due to uncaught exception 'NSInvalidArgumentException', 
        reason: 'Resource assets/mfx/my_sound.m4a can not be loaded'
    *** First throw call stack:
    (
    0   CoreFoundation     0x008ae746 __exceptionPreprocess + 182
    1   libobjc.A.dylib    0x02598a97 objc_exception_throw + 44
    2   CoreFoundation     0x008ae66d +[NSException raise:format:] + 141
    3   SpriteKit          0x011ca435 +[SKPlaySound playSoundFileNamed:atPosition:waitForCompletion:] + 628
    4   SpriteKit          0x011601b4 +[SKAction(SKActions) playSoundFileNamed:waitForCompletion:] + 78
    5   MyGame             0x0012eb25

Solution is: load all sounds only once, as a constants (let). Do not create variable each time you want to play the sound.

import SpriteKit
import AVFoundation

class Sounds
{
    static let SOUND1 = SKAction.playSoundFileNamed("assets/sound1.m4a", waitForCompletion: false)
    static let SOUND2 = SKAction.playSoundFileNamed("assets/sound2.m4a", waitForCompletion: false)
}

Then, in some SKSpriteNode for example:

func playSound1()
{
    self.runAction(Sounds.SOUND1)
}

Find similar / same issue here: Skaction.playsoundfilenamed crashes when repeat - sprite kit

Community
  • 1
  • 1
GeimF
  • 53
  • 5