【发布时间】:2014-05-07 05:53:09
【问题描述】:
当应用程序将退出活动状态时,我有一个通知设置来提醒我的 VC:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(pauseGame)
name:UIApplicationWillResignActiveNotification
object:[UIApplication sharedApplication]];
如您所见,它调用了一个名为 pauseGame 的方法。
该方法包括:
[audio pauseAudio];
我在应用程序中使用相同的方法暂停,一切正常。
当应用程序被发送到后台时,此方法被正确调用,但是,当应用程序恢复时,音频继续播放。其他暂停项目正在恢复,因此该方法正在完成。
如何让音频在进入后台时正确暂停?
更新:人们一直在提到 ApplicationDidEnterBackGroundNotification。我试过了,但它的工作原理是一样的。需要注意的是,Apple 在自己的 cmets 中的 AppDelegate 推荐了我原来使用的方法。有关他们在代表中预先放置的评论,请参见下文。
- (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.
}
更新 2:例如,当有来电时,使用 WillResignActive 可以正确暂停。通过该事件,音频会正确停止。因此,当按下主页按钮时,似乎只是应用程序进入后台。来电不会调用后台通知,因此显然呼叫和家庭按下会导致不同的状态。
【问题讨论】:
-
感谢您对格式的修改。
-
尝试使用“UIApplicationDidEnterBackgroundNotification” - 像这样:[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pauseGame) name:UIApplicationDidEnterBackgroundNotification object:nil];
-
与使用我上面发布的辞职声明相同的问题。
标签: ios cocoa-touch avaudioplayer nsnotificationcenter uiapplicationdelegate