how to increase and decrease the volume programmatically in android

后端 未结 7 1097
余生分开走
余生分开走 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:12

    Try below code

    audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    
    seekbar.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
    
    seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
      @Override
      public void onProgressChanged(SeekBar seekBar, int newVolume, boolean b) {
        textview.setText("Media Volume : " + newVolume);
        audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, newVolume, 0);
      }
    
      @Override
      public void onStartTrackingTouch(SeekBar seekBar) {}
    
      @Override
      public void onStopTrackingTouch(SeekBar seekBar) {}
    });
    

提交回复
热议问题