【问题标题】:AudioKit Background Audio Battery UsageAudioKit 后台音频电池使用情况
【发布时间】:2018-12-15 05:07:58
【问题描述】:

我刚从 AVAudio 切换到 AudioKit。为了让事情正常工作,我不得不启用背景音频,从那以后电池使用情况很糟糕。

在序列或播放器结束后,清理和退出背景音频的最佳方法是什么?

任何帮助将不胜感激。

【问题讨论】:

  • 这到底和编程有关吗?
  • 它肯定与编程有关。有一些技术可以检查您是否可以关闭引擎 - 例如,查看是否连接了任何 IAA。当我们本周末在 Synth One 中实施一些省电技术时,我将在下面写一个更一般的答案。

标签: ios swift background audiokit


【解决方案1】:

在您的应用委托中:

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()
}

【讨论】:

  • 感谢您提供此指导!追问:即使我们没有在集成AudioKit的应用程序中使用AudioBus集成,上述解决方案仍然适用吗?您还可以添加一些关于使用计时器触发并在将来再次检查的目的的细节吗?谢谢!
  • 当然,只是不要做你不需要的 IAA 的东西 - 只需管理引擎。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多