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

删除回忆录丶 提交于 2019-12-14 02:02:33

问题


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


回答1:


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/



来源:https://stackoverflow.com/questions/22978547/handling-interruptions-in-sprite-kit-cant-get-sound-effects-via-skaction-pla

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!