title: How to manage MediaPlayer.setVolume() + SeekBar to set it + device volume control
body: <p>As <a href="http://developer.android.com/reference/android/media/MediaPlayer.html#setVolume(float,%20float)" rel="noreferrer">recommended by the doc</a>, I'd like to use the <code>setvolume()</code> method to control the volume of my <code>MediaPlayer</code>, but I'm a bit confused about how to manage it.</p>
  
  <p>I'm setting the volume using a <code>SeekBar</code>, initialized to the current volume in the <code>AudioManager.STREAM_MUSIC</code> stream.</p>
  
  <p>This makes sense when the user does not modify the device volume control, but when he does, I'm not sure what I should do, and I have several doubts:</p>
  
  <ul>
  <li><p>how is the <code>MediaPlayer</code> volume related to the device volume? Does the max value (1) set in <code>setVolume()</code> correspond to <code>mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)</code></p></li>
  <li><p>if it's the case, the progress value of the <code>SeekBar</code> should be reset every time the device volume control is set, but then, what's the point of using <code>MediaPlayer.setVolume()</code> instead of <a href="http://developer.android.com/reference/android/media/AudioManager.html#setStreamVolume(int,%20int,%20int)" rel="noreferrer">AudioManager.setStreamVolume()</a>?</p></li>
  <li><p>what's the standard way of using <code>MediaPlayer.setVolume()</code> with a <code>SeekBar</code>?</p></li>
  </ul>
  
  <p></p>
  
  <pre><code>private final static int MAX_VOLUME = 100;
  private AudioManager mAudioManager; 
  private SeekBar mVolumeControl;
  
  mVolumeControl = (SeekBar) getActivity().findViewById(R.id.volume_control);
  mAudioManager = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
  mVolumeControl.setMax(MAX_VOLUME);
  mVolumeControl.setProgress(MAX_VOLUME*mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC)/mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)); 
  mVolumeControl.setOnSeekBarChangeListener(mVolumeControlChangeListener);
  
  private SeekBar.OnSeekBarChangeListener mVolumeControlChangeListener = new SeekBar.OnSeekBarChangeListener() {
  
      @Override
      public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
  
          float volume = (float) (1 - (Math.log(MAX_VOLUME - progress) / Math.log(MAX_VOLUME)));
          mediaPlayer.setVolume(volume, volume);
      }
  
      @Override
      public void onStartTrackingTouch(SeekBar seekBar) {}
  
      @Override
      public void onStopTrackingTouch(SeekBar seekBar) {
      }
  };
  </code></pre>