Current Android Release version:
Version Code: Pie
Version: 9
API level: 28

How to get/set system volume settings?

Audio Manager will help to get/set the volume settings.

public void onCreate(Bundle savedInstanceState) 
{
     super.onCreate(savedInstanceState);
     setContentView(R.layout.volume);

     AudioManager mAudio =                                                                                   (AudioManager)getSystemService(Context.AUDIO_SERVICE);

    SeekBar alarm = (SeekBar)findViewById(R.id.alarm);
    SeekBar music = (SeekBar)findViewById(R.id.music);
    SeekBar ring = (SeekBar)findViewById(R.id.ring);
    SeekBar system = (SeekBar)findViewById(R.id.system);
    SeekBar voice = (SeekBar)findViewById(R.id.voice);

    initControls(alarm, AudioManager.STREAM_ALARM);
    initControls(music, AudioManager.STREAM_MUSIC);
    initControls(ring, AudioManager.STREAM_RING);
    initControls(system, AudioManager.STREAM_SYSTEM);
    initControls(voice, AudioManager.STREAM_VOICE_CALL);

}

private void initControls (SeekBar seek, final int stream) 

{

   seek.setMax(mAudio.getStreamMaxVolume(stream));
   seek.setProgress(mAudio.getStreamVolume(stream));

   seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()    {
       public void onProgressChanged(SeekBar bar, int progress, boolean                                                      fromUser) 
       {
              mAudio.setStreamVolume(stream, progress,                                                                                          AudioManager.FLAG_PLAY_SOUND);
       }
       public void onStartTrackingTouch(SeekBar bar) {
       }
       public void onStopTrackingTouch(SeekBar bar) {
       }
  });
}

No comments:

Post a Comment