Android volume control

I would like to know how to control the volume of my application with the volume keys (contrary to my belief, I read that they control only the ringer volume). Should I rewrite onKey Down / Up?

Or is there any other way to do this? I ask, because if I rewrote the above function for an action, the functions will receive the event only if the view associated with this action has focus, and I'm looking for something "Global" (so that the question doesnโ€™t work, which activity is running now)

+35
android
Mar 29 '10 at 15:44
source share
4 answers

There was another question a long time ago that asked the same thing. Essentially, the answer: do not override the onKeyDown and onKeyUp buttons. It is much better to just use this single line setVolumeControlStream(AudioManager.STREAM_MUSIC); in your onCreate () method. This tells the operating system that the volume buttons should affect the volume of the "media" when your application is visible, and the one that it uses for your application.

As for managing the volume of multimedia, no matter which application is visible, I'm not sure if this can be done - or, if possible, whether it will be good.

+66
Mar 29 '10 at 16:02
source share

In your activity, you can use one of the following actions:

 this.setVolumeControlStream(AudioManager.STREAM_MUSIC); this.setVolumeControlStream(AudioManager.STREAM_RING); this.setVolumeControlStream(AudioManager.STREAM_ALARM); this.setVolumeControlStream(AudioManager.STREAM_NOTIFICATION); this.setVolumeControlStream(AudioManager.STREAM_SYSTEM); this.setVolumeControlStream(AudioManager.STREAM_VOICECALL); 
+9
Jun 06 2018-12-06T00:
source share

We hope that this working Android code will help you develop your own volume management application:

 public class audioSetting extends Activity { Button b; TextView t; SeekBar s; EditText e; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b = (Button)findViewById(R.id.button1); s = (SeekBar)findViewById(R.id.seekBar1); e = (EditText)findViewById(R.id.editText1); b.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub finish(); } }); final AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE); int a = audioManager.getStreamMaxVolume(AudioManager.STREAM_RING); int c = audioManager.getStreamVolume(AudioManager.STREAM_RING); b.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub audioManager.setStreamVolume(AudioManager.STREAM_RING, (int)(Integer.parseInt(e.getText().toString().trim())), 0); s.setProgress((int)(Integer.parseInt(e.getText().toString().trim()))); } }); s.setMax(a); s.setProgress(c); e.setText(""+c); s.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar arg0) { } @Override public void onStartTrackingTouch(SeekBar arg0) { } @Override public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) { audioManager.setStreamVolume(AudioManager.STREAM_RING, arg1, 0); e.setText(""+s.getProgress()); } }); } } 
+1
May 28 '15 at 7:50
source share

This is an old question, but there is an excellent volume control library in Android (even better than the volume buttons, which are limited to 5 increments in volume level).

http://code.google.com/p/media-volume-control

Take a look at this package , it provides a continuous volume level, as well as other nice features (for example, when the sound is interrupted, the volume monitor to be notified if / when the volume level is changed by any means - manual when displaying the SeekBar volume, etc.).

0
Feb 10 '12 at 3:12
source share



All Articles