45

I'd like to know how to control my application's volume from the volume keys (contrary to my belief , I've read they control only the ringer volume). Should I overwrite the onKey Down/Up?

Or is there other way to accomplish this? I'm asking because if I overwrite the upper mentioned function for an activity, then the functions will receive the event only if a view associated with the this activity has the focus, and I'm looking for something "Globaly" ( to work no matter what activity is running now)

4 Answers 4

79

There was another question from a long time ago that asked the same thing. Essentially the answer is: don't override the onKeyDown and onKeyUp buttons. It's much better to simply use this one line setVolumeControlStream(AudioManager.STREAM_MUSIC); in your onCreate() method. That tells the OS that the volume buttons should affect the "media" volume when your application is visible, and that's the volume it uses for your application.

As for controlling the Media volume no matter what app is visible, I'm not sure that can be done - or if it could, whether that would be a good thing.

8
  • The thing is that I'm interested to function only when activities of my application are running, I don't have any desire to make it work ,if an activity from other app is at the top of the activity stack Mar 29, 2010 at 16:14
  • Thanks ! I guess it could work for me ! Is there any suggestion in what activity should this line be added ? Mar 29, 2010 at 16:16
  • Put it in your onCreate() method of any activity that's going to be making sound. Mar 29, 2010 at 16:25
  • By the way, you should look through the questions you've asked, and mark answers as "accepted" if the answers helped you. This is a nice thing to do for the people who helped you as it gives them 'reputation' - the number shown below our usernames. Read the FAQ for more detail. Mar 29, 2010 at 16:40
  • By the way , If I catch the event of pressing down the volume keys in onKeyDown , and return super.onKeyDown , do I distract the android sound system ? ....I just want to be informed of such a press without any other interaction with the sound Mar 29, 2010 at 16:47
14

In your activity you can use one of the following:

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);  
3

Hope this working android code will help you to develop your own volume control 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());
                }
            });
        }
}
0

This is an old question but there is a superb library for dealing with volume control in android (even better than the volume buttons, which are limited to 5 increments of volume level).

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

Take a look at this package, it provides continuous volume level as well as other nice features (such as when audio is interrupted, volume monitor to be notified if/when the volume level is changed by whatever means--handdy when showing a volume SeekBar, etc.).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.