Whirlwind
Whirlwind

Reputation: 13675

SKAction playSoundFileNamed doesn't work after receiving two consecutive phone calls

I am trying to figure out why playSoundFileNamed doesn't work after receiving two consecutive phone calls. Actually it works only after the first phone call is received. Reproducing steps are:

  1. Start a game
  2. Wait for a phone call and go to background
  3. Phone call is finished (declined or interrupted by caller)
  4. Returning to foreground

After this, playing the sound from touchesBegan still works.

When I repeat the steps from above (first step is skipped), mechanism from touchesBegan stops working. Not sure why is this happening...Here is the code which can produce described behaviour:

@interface GameScene()

@property (nonatomic, strong) SKAction *sound;
@end

@implementation GameScene

-(void)didMoveToView:(SKView *)view {
    self.sound = [SKAction playSoundFileNamed:@"sound1.wav" waitForCompletion:NO];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch begins */
    [self runAction:self.sound];
}
@end

I know there are some questions related to this on SO, but given answers are related to workarounds. I am not interested in workaround, but rather why is this happening? Is it somehow related to AVAudioSession ? (probably not) I know I could use AVAudioPlayer as a workaround, but still not sure how much is that performant for playing a lot of simple short sounds...

Upvotes: 12

Views: 550

Answers (2)

neave
neave

Reputation: 2531

This could be an issue with mixed audio. I've found that enabling mixed audio no longer stops SpriteKit's audio engine during an interruption, ad video playing, or app backgrounding.

try? AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)

Upvotes: 0

sangony
sangony

Reputation: 11696

The SKAction playSoundFileNamed is buggy in regards to the app transitioning between background and foreground. That's the reason why you are having this issue. I am not sure if this problem has been corrected in iOS 9.

As for a workaround, you stated you're not interest but I will include one for completion's sake. Use AVAudioPlayer instead of SKAction. AVAP has the ability to stop and start (using its delegates) based on your app's state.

Upvotes: 2

Related Questions