AudioKit背景音频电池使用

问题描述 投票:6回答:1

我刚从AVAudio切换到AudioKit。为了让事情有效,我必须启用背景音频,因为那时电池使用率很低。

一旦序列或玩家进入和结束,清理和播放背景音频的最佳方法是什么?

任何帮助将不胜感激。

ios swift background audiokit
1个回答
4
投票

在您的app委托中:

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()
}
© www.soinside.com 2019 - 2024. All rights reserved.