Search code examples
javaandroidvolume

How to control volume with custom Button (not Android devices buttons)


Hello i cant find an answer to my question. I create application to control an Android TV, and also i create a Buttons for control a volume UP, DOWN, but how to do this?

Its not problem for me to send a byte to TV, but i dont know how to control the volume, specific with Up and Down.

Can u help me guys?


Solution

  • How about this

    //Create an AudioManager 
        AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    //Increase the volume
        audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
    //Decrease the volume
        audio.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);