Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 778
Whirlwind Avatar asked Sep 10 '15 17:09

Whirlwind


1 Answers

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.

like image 153
sangony Avatar answered Oct 20 '22 14:10

sangony