2

I have the following code for my SeekBar:

setVolumeControlStream(AudioManager.STREAM_MUSIC);

            final AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
            int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
            int curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);

            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View layout = inflater.inflate(R.layout.custom_dialog, (ViewGroup) findViewById(R.id.dialog_root_element));


            SeekBar volControl = (SeekBar)alert.findViewById(R.id.dialog_seekbar);

            volControl.setMax(maxVolume);
            volControl.setProgress(curVolume);
            volControl.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_MUSIC, arg1, 0);
                }
            });

The problem is that I would like when user decide use volume device buttons, my SeekBar up or down too.

Actually, with this code I can control volume with both methods (device buttons and code) correctly,but when I use volume device buttons, my SeekBar is static.

Any ideas or suggestions?

Thank you!

Hesham Saeed
  • 5,358
  • 7
  • 37
  • 57
user1364684
  • 800
  • 2
  • 8
  • 28

2 Answers2

1

you can solve this problem by reading the following discussions.

Is there a listener to listen for changes in the volume in android?

Taking over the volume key on Android

Hope this helps.

Community
  • 1
  • 1
Vinay W
  • 9,912
  • 8
  • 41
  • 47
1

Check Following Answer :

  public boolean onKeyDown(int keyCode, KeyEvent event) {
          if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) 
         { 
              mediaVlmSeekBar = (SeekBar) findViewById(R.id.seekBar1);
                int index = mediaVlmSeekBar.getProgress(); 
                mediaVlmSeekBar.setProgress(index + 1); 
                return true; 
         } else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)
          {
                int index = mediaVlmSeekBar.getProgress(); 
                mediaVlmSeekBar.setProgress(index - 1); 
                return true; 
         }
          return super.onKeyDown(keyCode, event); 
         }
Yogesh Mane
  • 403
  • 6
  • 18