iOS 7.1 Sprite Kit AVAudioSession Crash when enter background

烈酒焚心 提交于 2019-12-03 09:00:17

I did it!

I just paused the SKView in- (void)applicationWillResignActive:(UIApplication *)application and included the AVAudioSession set to inactive.

AppDelegate.h

   #import <SpriteKit/SpriteKit.h>

AppDelegate.m

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

SKView *view = (SKView *)self.window.rootViewController.view;
view.paused = YES;
 }


  - (void)applicationDidEnterBackground:(UIApplication *)application
   {

[[AVAudioSession sharedInstance] setActive:NO error:nil];
}


  - (void)applicationDidBecomeActive:(UIApplication *)application
   {
SKView *view = (SKView *)self.window.rootViewController.view;
view.paused = NO;
[[AVAudioSession sharedInstance] setActive:YES error:nil];
 }

adding to @ObjectivesCsam answer:

when rootViewControllers view is not SKView u can use

- (SKView *)getGameView {
    NSArray *viewControllers = self.window.rootViewController.childViewControllers;
    for (UIViewController *vc in viewControllers) {
        if ([vc.view isKindOfClass:[SKView class]]) {
            SKView *view = (SKView *)vc.view;
            return view;
        }
    }
    return nil;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!