iOS 7.1 Sprite Kit AVAudioSession Crash when enter background

≡放荡痞女 提交于 2019-12-09 07:05:17

问题


So 2 weeks ago I submitted a sprite kit app to the app store and it all was fine. I was having problems before i submitted the app where it would crash because of AvAudioSession, however i was able to fix that problem through this Sprite Kit & playing sound leads to app termination. This basically sets AVAudioSession to inactive when going into background and then active again when coming into foreground. I recently update my phone it iOS 7.1 and this fix doesn't seem to work in the new 7.1 and my app is again crashing whenever it enters the background. I have taken all the audio out of my app and it seems to work fine so it is the same problem as i had before just now the solution doesn't work!I really need to fix this problem as i have an update ready to submit.. Cheers Sam


回答1:


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];
 }



回答2:


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;
}


来源:https://stackoverflow.com/questions/22407111/ios-7-1-sprite-kit-avaudiosession-crash-when-enter-background

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