且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

雪碧套件播放声音导致应用程序终止

更新时间:2023-02-01 18:55:30

问题是 AVAudioSession 在应用进入后台时无法激活.这不是很明显,因为 Sprite Kit 没有提到它在内部使用 AVAudioSession.

The problem is AVAudioSession can't be active while the app enters background. This isn't immediately obvious because Sprite Kit makes no mention that it uses AVAudioSession internally.

修复非常简单,也适用于 ObjectAL => 在应用程序处于后台时将 AVAudioSession 设置为不活动,并在应用程序进入前台时重新激活音频会话.

The fix is quite simple, and also applies to ObjectAL => set the AVAudioSession to inactive while the app is in background, and reactivate the audio session when the app enters foreground.

经过此修复的简化 AppDelegate 如下所示:

A simplified AppDelegate with this fix looks like so:

#import <AVFoundation/AVFoundation.h>
...

- (void)applicationWillResignActive:(UIApplication *)application
{
    // prevent audio crash
    [[AVAudioSession sharedInstance] setActive:NO error:nil];
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // prevent audio crash
    [[AVAudioSession sharedInstance] setActive:NO error:nil];
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // resume audio
    [[AVAudioSession sharedInstance] setActive:YES error:nil];
}

PS:此修复将包含在 Kobold Kit v7.0.3 中.

PS: this fix will be included in Kobold Kit v7.0.3.