【问题标题】:Background Music not replaying in iOS8 when entering foreground进入前台时,iOS 8 中不播放背景音乐
【发布时间】:2023-11-04 09:21:01
【问题描述】:

停用正在运行 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,我想知道如何解决此问题。

【问题讨论】:

  • 我不为移动平台开发,但您不应该尝试使用适用于 iOS 8 的 SDK 的更新版本吗?
  • @zyboxinternational 你这是什么意思?很抱歉听起来像个业余爱好者。
  • 如,iOS 8 可能需要更新的 SDK 以启用对新功能的访问,并提供与现有功能的兼容性。
  • 我很确定我有最新的 SDK,我使用的是 xcode6。我只想知道为什么代码(适用于 iOS7)不再适用于 iOS8,我是否必须在 Swift 中重写我的所有代码?

标签: ios8 avaudioplayer appdelegate


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    Background Modes中选择Audio and Airplay项目,这对我有用。

    【讨论】: