iOS 8如何在Sprite Kit中暂停和恢复音频

时间:2014-10-30 22:40:33

标签: ios objective-c xcode audio avaudiosession

我环顾四周,当应用程序在 iOS 8 中进入后台时,我似乎无法找到如何恢复音频。这就是我的应用代表:

 #import "AppDelegate.h"
 @interface AppDelegate ()

 @end
 @implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:     (NSDictionary *)launchOptions {
// Override point for customization after application launch.
return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
[[AVAudioSession sharedInstance] setActive:NO error:nil];
SKView *view = (SKView *)self.window.rootViewController.view;
view.paused = YES;
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
[[AVAudioSession sharedInstance] setActive:NO error:nil];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
[[AVAudioSession sharedInstance] setActive:YES error:nil];
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
SKView *view = (SKView *)self.window.rootViewController.view;
view.paused = NO;
[[AVAudioSession sharedInstance] setActive:YES error:nil];
}

- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

问题是,当应用程序进入后台时,音频不会在ios 8中继续播放。它会返回此错误:

错误:

 [0x35ad49dc] AVAudioSession.mm:646: -[AVAudioSession setActive:withOptions:error:]: Deactivating an audio session that has running I/O. All I/O should be stopped or paused prior to deactivating the audio session.

ios7中不存在此问题。音频像SKAction一样播放:

 SKAction *music = [SKAction playSoundFileNamed:@"new music.mp3" waitForCompletion:NO];
[[self scene] runAction:music];

有人可以告诉我如何在iOS8中正确暂停和恢复音频吗?

0 个答案:

没有答案
相关问题