12

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...

Justin Levi Winter
  • 2,327
  • 2
  • 17
  • 30
Whirlwind
  • 14,286
  • 11
  • 68
  • 157
  • 2
    I was dealing with this quite a while ago. I never found a solution. For me it would only take one phone call though. But there was a worse situation that you may want to check for also. I was using iAd and every time an ad that had sound (like those game of war ads) was presented, this too would stop playSoundFileNamed: from working until the app was relaunched. The only thing I could do was remove ads from my game. I couldn't risk the user experience of having all the game sound effects fail if they happened to get an ad with audio. And I had to just hope users don't get a call. – Brad Apr 18 '16 at 00:40

2 Answers2

2

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.

sangony
  • 11,636
  • 4
  • 39
  • 55
  • 1
    Yeah, playSoundFileNamed acts buggy and probably at the moment of speaking we can't do anything to change that. I guess I could fill a radar about this. – Whirlwind Sep 20 '15 at 10:09
0

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)
neave
  • 2,482
  • 1
  • 26
  • 18
  • neave - I tried your solution and just tested, but it didn't work for me. There is a lot of info on this subject but it is very dated. Is this still working for your app? I can't switch - whenever I go to other sound engines, run into a performance issue. Thanks for any info – Voltan Jun 19 '18 at 19:47