ios - AudioKit 后台音频电池使用

标签 ios swift background audiokit

我刚从 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/

相关文章:

ios - 如何获取带有换行符(\n)的 UITextView 的内容高度?

ios - xcode中 "undefined symbols for architecture x86_64"和 "linker command failed with exit code 1"如何解决?

swift - 使用 swift 在 CloudKit 中加载更多内容

CSS - 黑色背景上的白色文本,看起来更大胆

Android背景图片内存使用情况

javascript - 容器应自动调整到背景图像的高度,仅适用于响应式设计 CSS

iOS 类选择 NSDictionary 或 Core Data

ios - CGPDFStringGetBytePtr 在扫描 pdf 时返回不正确的字符串

ios - 类型 'Data' 的值没有成员 'writeToURL'

Swift 覆盖子类的所有 setter 和 getter