Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 通話中關機鈴聲無法從外放輸出的修改方法

編輯:關於 Android 程式設計

這個問題出在音訊路由策略上:當串流型別為 BOOT(關機鈴聲)時,系統沒有明確指定輸出裝置,導致聲音無法從喇叭播放。解法是在 AudioMTKPolicyManager.cpp 的 startOutput 方法中,取得 newDevice 之後強制補上喇叭裝置。

關鍵改動只有一行:

if (stream == AudioSystem::BOOT) newDevice |= AUDIO_DEVICE_OUT_SPEAKER;

放到上下文裡看:

status_t AudioMTKPolicyManager::startOutput(audio_io_handle_t output,
                                           AudioSystem::stream_type stream,
                                           int session)
{
    ALOGD("startOutput() output %d, stream %d, session %d", output, stream, session);
    ......
    if (outputDesc->mRefCount[stream] == 1) {
        audio_devices_t newDevice = getNewDevice(output, false /*fromCache*/);
        routing_strategy strategy = getStrategy(stream);
        bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
                          (strategy == STRATEGY_SONIFICATION_RESPECTFUL);
        uint32_t waitMs = 0;
        bool force = false;
        // 關機鈴聲強制走喇叭
        if (stream == AudioSystem::BOOT) newDevice |= AUDIO_DEVICE_OUT_SPEAKER;
        for (size_t i = 0; i < mOutputs.size(); i++) {
            AudioOutputDescriptor *desc = mOutputs.valueAt(i);
            if (desc != outputDesc) {
                // 若同一個硬體模組下的其他輸出仍選著不同裝置,
                // 需強制觸發一次裝置切換,讓 audio HAL 收到新的裝置選擇。
                if (outputDesc->sharesHwModuleWith(desc) &&
                    desc->device() != newDevice) {
                    force = true;
                }
                // 播放通知類音效時,等待其他正在活動的輸出先完成呈現,
                // 讓音訊焦點效果能正確傳遞。
                uint32_t latency = desc->latency();
                if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
                    waitMs = latency;
                }
            }
        }
        ......
    }
    return NO_ERROR;
}

修改原理

getNewDevice() 依據串流型別與當前路由策略推算出目標輸出裝置,但 BOOT 這種較少見的串流型別並未被涵蓋,回傳的裝置遮罩中沒有喇叭位。在算出結果後以位元或運算補上 AUDIO_DEVICE_OUT_SPEAKER,路由策略後續就能把音訊導向喇叭。這種寫法的優點是侵入性極小,不影響其他串流型別的既有行為。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved