user1615898
user1615898

Reputation: 1233

SpriteKit playSoundFileNamed crash on iOS 8.3

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)

Upvotes: 9

Views: 582

Answers (3)

omer15
omer15

Reputation: 134

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)")
    }
}

Upvotes: 1

GeimF
GeimF

Reputation: 55

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

Upvotes: 0

Justin Buergi
Justin Buergi

Reputation: 103

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

Upvotes: 2

Related Questions