gpt4 book ai didi

ios - 当 App 进入后台时暂停音频

转载 作者:行者123 更新时间:2023-11-29 02:59:07 25 4
gpt4 key购买 nike

我有一个通知设置,当应用程序要退出事件状态时提醒我的 VC:

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(pauseGame)
name:UIApplicationWillResignActiveNotification
object:[UIApplication sharedApplication]];

如您所见,它调用了一个名为 pauseGame 的方法。
该方法除其他外,包含:

[audio pauseAudio];

我在应用程序中使用相同的方法暂停,一切正常。

当应用程序被发送到后台时,此方法被正确调用,但是,当应用程序恢复时,音频继续播放。其他暂停项正在恢复,因此该方法正在完成。

进入后台时如何让音频正常暂停?

更新:人们一直在提到 ApplicationDidEnterBackGroundNotification。我试过了,但效果一样。需要注意的是,Apple 在 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 可以在来电时正确暂停。有了那个事件,音频就会正确停止。所以这似乎只是按下主页按钮时应用程序进入后台。来电不会调用后台通知,因此很明显,调用和按主页会导致不同的状态。

最佳答案

    - (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, called instead of applicationWillTerminate: when the user quits.
*/
NSLog(@"Application moving to background");
// Here Call your notification for pause audio
}

https://developer.apple.com/library/ios/documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html

关于ios - 当 App 进入后台时暂停音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23509755/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com