3

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?

Edna Krabappel
  • 362
  • 1
  • 2
  • 16

1 Answers1

0

Android TV does not expose volume_up/volume_down buttons to the application.

The TV controls are shown as a D-Pad: see D-pad minimum controls

This up/down events from the remote / hardware buttons are handled by the TV hardware before they get to Android (makes sense since the TV might have other functions that depend on).

You can control you app volume output, and raise/lower volume and mute the TV from ADB; but you cannot get this events.

How to handle sound on TVs? The TV has internally a mixer. Output an appropiate sound level (not at 100% to avoid distorsion in some equipment).

Iñigo González
  • 3,735
  • 1
  • 11
  • 27