how to increase and decrease the volume programmatically in android

后端 未结 7 1099
余生分开走
余生分开走 2020-12-08 22:54

I created the music player app and i want to set the volume up/down programmatically .

I want to implement two another

相关标签:
7条回答
  • 2020-12-08 23:28

    if your type is AudioManager.STREAM_MUSIC: use this code ,follow phone volume button

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
        case KeyEvent.KEYCODE_VOLUME_UP:
            audio.adjustStreamVolume(
                AudioManager.STREAM_MUSIC,
                AudioManager.ADJUST_RAISE,
                AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
            return true;
        case KeyEvent.KEYCODE_VOLUME_DOWN:
            audio.adjustStreamVolume(
                AudioManager.STREAM_MUSIC,
                AudioManager.ADJUST_LOWER,
                AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
            return true;
        default:
            break;
        }
        return super.onKeyDown(keyCode, event);
    }
    
    0 讨论(0)
提交回复
热议问题