Thursday 7 January 2021

Why AudioManager works for Phone, but not for Android TV emulator?

As one can read here, I try to adjust the volume. Here I set the AudioManager:

audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager

and I react on a onKeyDown() like:

override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
    when (event?.keyCode) {
        KeyEvent.KEYCODE_VOLUME_UP -> {
            adjustVolume()
            audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE)
            return true
        }
        KeyEvent.KEYCODE_VOLUME_DOWN -> {
            adjustVolume()
            audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE)
            return true
        }
        else -> {
            return super.onKeyDown(keyCode, event)
        }
    }
}

and adjustVolume() displays then the volume value. This works so far on phones. I can adjust the volume with my own UI and I can see log output.

But when I run this app on a Android TV Emulator, linked code does not work and I see stock UI for volume.

According to @I~nigos answer, the kernel "consumes" the keyevent already at TVs hardware level. It was proposed to handles the sound from internal mixer.

However, how can I intercept the sound level. Also by intercepting the internal mixer.. But how?



from Why AudioManager works for Phone, but not for Android TV emulator?

No comments:

Post a Comment