首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当进入前台时,背景音乐不能在iOS8中重放

当进入前台时,背景音乐不能在iOS8中重放
EN

Stack Overflow用户
提问于 2014-09-19 02:28:07
回答 2查看 638关注 0票数 2

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

代码语言:javascript
复制
- (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上就不行了,我想知道如何解决这个问题。

EN

回答 2

Stack Overflow用户

发布于 2014-09-30 00:06:24

我也有同样的问题。我还可以在退出时暂停和暂停音频-在iOS7中工作得很好-但iOS8不能工作。我在ios7和ios8设备上都尝试过xcode6和beta xcode6.1。ios7从来没有出现过问题,并按预期工作-但ios8给我带来了问题,不会暂停或暂停音频。真的很烦人!

票数 0
EN

Stack Overflow用户

发布于 2014-12-28 22:47:21

Background Modes中选择音频和播放项目,这对我很有效。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25919844

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档