背景音乐在进入前景时无法在iOS 8中播放

时间:2014-09-18 18:28:07

标签: ios8 avaudioplayer appdelegate

停用已运行I / O的音频会话。在停用音频会话之前,应停止或暂停所有I / O.

- (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.
    SKView *view = (SKView *)self.window.rootViewController.view;
    ((MyScene *)view.scene).gamePaused = YES;

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

- (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 active state; here you can undo many of the changes made on entering the background.
    [[AVAudioSession sharedInstance] setActive:YES error:nil];
}

这是我在AppDelegate.m文件中的代码。每当我退出应用程序并重新进入时,我的游戏的背景音乐都不会再次开始重播。这段代码适用于iOS7,但对于iOS8不再适用,我想知道如何解决这个问题。

2 个答案:

答案 0 :(得分:0)

我有同样的问题。退出时我也有停顿并暂停音频 - 在iOS7中工作得很好 - 但是ios 8不起作用..我在ios7和ios8设备上尝试了xcode6和beta xcode6.1。 ios7从来没有问题,按预期工作 - 但ios8给了我问题,不暂停或暂停音频..真烦人!!

答案 1 :(得分:0)

背景模式中选择音频和Airplay 项目,这对我有用。