Volume Control in android application

后端 未结 4 1738
既然无缘
既然无缘 2020-11-27 02:29

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 t

相关标签:
4条回答
  • 2020-11-27 02:58

    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.

    0 讨论(0)
  • 2020-11-27 03:00

    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 讨论(0)
  • 2020-11-27 03:06

    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.).

    0 讨论(0)
  • 2020-11-27 03:19

    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);  
    
    0 讨论(0)
提交回复
热议问题