简体   繁体   English

处理Sprite Kit中的中断-无法通过[SKAction playSoundFileNamed:获得声音效果]

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

Handling interruptions in Sprite Kit - can't get sound effects via [SKAction playSoundFileNamed: to work after interruption (like a phone call) 处理Sprite Kit中的中断-无法通过[SKAction playSoundFileNamed:获得声音效果]中断后可以正常工作(例如通话)

Was able to get background music to restart after interruption by adding AVAudioPlayerDelegate to my GameScene.h then adding to my GameScene.m 通过将AVAudioPlayerDelegate添加到我的GameScene.h,然后添加到我的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 当中断开始时在AppDelegate和GameScene中都运行,当中断没有运气时结束时,setActive:YES,在中断结束后声音效果不会产生声音

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. 简而言之,如果您遇到这些问题,并且看起来确实如此,那就是跳过SKAction playSoundFileName并纯粹使用AVAudioPlayer。

You would have to: 您将必须:

  1. Only use AVAudioPlayer for all your audio as it is designed and tested to handle interruptions. 经过专门设计和测试以处理中断,请仅对所有音频使用AVAudioPlayer。
  2. Use NSData as a sound container to be used with AVAudioPlayer 使用NSData作为与AVAudioPlayer一起使用的声音容器
  3. Each sound should have its own temporary AVAudioPlayer instance for sound lifetime only. 每个声音都应具有其自己的临时AVAudioPlayer实例,仅用于声音寿命。

There is a detailed explanation of how to do this on http://iknowsomething.com/ios-sdk-spritekit-sound/ 有关如何执行此操作的详细说明,请访问http://iknowsomething.com/ios-sdk-spritekit-sound/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM