我刚从 AVAudio 切换到 AudioKit。为了让一切正常运行,我必须启用背景音频,从那时起电池使用情况就很糟糕。
一旦序列或播放器开始和结束,清理和退出背景音频的最佳方法是什么?
如有任何帮助,我们将不胜感激。
最佳答案
在您的应用委托(delegate)中:
func applicationDidEnterBackground(_ application: UIApplication) {
conductor.checkIAAConnectionsEnterBackground()
}
func applicationWillEnterForeground(_ application: UIApplication) {
conductor.checkIAAConnectionsEnterForeground()
}
在您的音频引擎或导体中(根据其他 AudioKit 示例)执行:
var iaaTimer: Timer = Timer()
func checkIAAConnectionsEnterBackground() {
if let audiobusClient = Audiobus.client {
if !audiobusClient.isConnected && !audiobusClient.isConnectedToInput {
deactivateSession()
AKLog("disconnected without timer")
} else {
iaaTimer.invalidate()
iaaTimer = Timer.scheduledTimer(timeInterval: 20 * 60,
target: self,
selector: #selector(self.handleConnectionTimer),
userInfo: nil, repeats: true)
}
}
}
func checkIAAConnectionsEnterForeground() {
iaaTimer.invalidate()
startEngine()
}
func deactivateSession() {
stopEngine()
do {
try AKSettings.session.setActive(false)
} catch let error as NSError {
AKLog("error setting session: " + error.description)
}
iaaTimer.invalidate()
AKLog("disconnected with timer")
}
@objc func handleConnectionTimer() {
AKLog("should disconnect with timer")
deactivateSession()
}
关于ios - AudioKit 后台音频电池使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51212955/