Handling interruptions in Sprite Kit - can't get sound effects via [SKAction playSoundFileNamed: to work after interruption

StackOverflow https://stackoverflow.com/questions/22978547

Frage

Handling interruptions in Sprite Kit - can't get sound effects via [SKAction playSoundFileNamed: to work after interruption (like a phone call)

Was able to get background music to restart after interruption by adding AVAudioPlayerDelegate to my GameScene.h then adding to my GameScene.m

-(void)audioPlayerBeginInterruption:(AVAudioPlayer *)player
{
        [_backgroundAudioPlayer pause];

}

-(void)audioPlayerEndInterruption:(AVAudioPlayer *)player
{
    [_backgroundAudioPlayer prepareToPlay];
    [_backgroundAudioPlayer play];

}

I've tried adding

[[AVAudioSession sharedInstance] setActive:NO error:nil];

Both in AppDelegate and GameScene when interruptions starts and setActive:YES when interruption ends with no luck, sound effects generate no sound after interruption ends

War es hilfreich?

Lösung

From various discussions this seems to be a bug which can show its head on occasion. The short of it is that if you are experiencing these issues, and it appears you are, it comes down to skipping the SKAction playSoundFileName and going purely with AVAudioPlayer.

You would have to:

  1. Only use AVAudioPlayer for all your audio as it is designed and tested to handle interruptions.
  2. Use NSData as a sound container to be used with AVAudioPlayer
  3. Each sound should have its own temporary AVAudioPlayer instance for sound lifetime only.

There is a detailed explanation of how to do this on http://iknowsomething.com/ios-sdk-spritekit-sound/

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top