Change volume in Android?

Can I change the volume of multimedia? And How? I have used this so far:

setVolumeControlStream(AudioManager.STREAM_MUSIC); 

But you have an arrow and you want to change the volume of the medium, not the volume of the ringer.

So can someone show me how easy it is to change the media size in onCreate() and I will fix the link later.

+49
android audio android-audiomanager
Nov 14 '10 at 18:38
source share
6 answers

The correct use method would be setStreamVolume on your AudioManager . It might look like this.

 AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE); audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, [int value], [if desired a flag]); 

An example of using a flag is to receive a sound when setting up a volume so that the user can hear the result. The flag for this will be AudioManager.FLAG_PLAY_SOUND .

You can use AudioManager.FLAG_SHOW_UI if you do not want to play the sound, but display a toast with the current value. Use should get feedback. It doesnโ€™t matter, audibly or visually.

To get the maximum allowable value for a given stream, you simply call getStreamMaxVolume() on the AudioManager and get the integer back, which represents ... well, the maximum allowable value for the volume.

+84
Nov 14 '10 at 20:23
source share
 private AudioManager audio; 

Inside onCreate:

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

Override onKeyDown:

 @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_SHOW_UI); return true; case KeyEvent.KEYCODE_VOLUME_DOWN: audio.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI); return true; default: // return false; // Update based on @Rene comment below: return super.onKeyDown(keyCode, event); } } 
+36
Mar 27 '11 at 0:22
source share

You can use the following code to process the volume with the SeekBar:

 AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE); SeekBar sbVolumeBooster = (SeekBar) findViewById(R.id.sbVolumeBooster); sbVolumeBooster.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)); sbVolumeBooster.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)); sbVolumeBooster.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar arg0) { } @Override public void onStartTrackingTouch(SeekBar arg0) { } @Override public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) { audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0); // 0 can also be changed to AudioManager.FLAG_PLAY_SOUND } }); 
+8
Jan 15 '15 at 14:20
source share

Providing 0 - in the flags avoids the visual and sound indicator. This is good when you implement your own sound bar and indicator, and you do not want the android to add anything.

+3
May 31 '14 at 15:52
source share

Use adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, flags);

http://developer.android.com/reference/android/media/AudioManager.html#adjustStreamVolume(int , int, int)

+1
Nov 14 '10 at 18:45
source share
 final SeekBar volumnRocker=findViewById(R.id.seekBar); volumnRocker.setMax(maxVolumn); volumnRocker.setProgress(currentVolumn); volumnRocker.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean b) { volumnProgress=progress; } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,volumnProgress,0); } }); 

he is currently changing the volume of audioManger using the search bar. How can I change the progress of the search bar using the volume button?

0
Jul 10 '19 at 16:28
source share



All Articles