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)
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
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)")
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With