SKAction playSoundFileNamed 连续接到两个电话后不起作用 [英] SKAction playSoundFileNamed doesn't work after receiving two consecutive phone calls

查看:17
本文介绍了SKAction playSoundFileNamed 连续接到两个电话后不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚为什么 playSoundFileNamed 在接到两个连续电话后不起作用.实际上它只有在接到第一个电话后才起作用.复制步骤是:

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. 开始游戏
  2. 等待电话并转到后台
  3. 电话已结束(被来电者拒绝或打断)
  4. 返回前台

在此之后,从 touchesBegan 播放声音仍然有效.

After this, playing the sound from touchesBegan still works.

当我重复上面的步骤(跳过第一步)时,touchesBegan 的机制停止工作.不知道为什么会发生这种情况......这是可以产生所描述行为的代码:

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

我知道在 SO 上有一些与此相关的问题,但给出的答案与解决方法有关.我对解决方法不感兴趣,而是为什么会发生这种情况?它是否与 AVAudioSession 有某种关系?(可能不是)我知道我可以使用 AVAudioPlayer 作为一种解决方法,但仍然不确定播放大量简单短声音的性能如何......

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

推荐答案

SKAction playSoundFileNamed 在应用程序在后台和前台之间转换方面存在问题.这就是您遇到此问题的原因.我不确定这个问题是否已在 iOS 9 中得到纠正.

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.

至于解决方法,您说您不感兴趣,但为了完整起见,我将包括一个.使用 AVAudioPlayer 而不是 SKAction.AVAP 能够根据您应用的状态停止和启动(使用其委托).

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.

这篇关于SKAction playSoundFileNamed 连续接到两个电话后不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆