2014-01-05 42 views
0

好吧,所以我讓我的Seek欄改變音量,該部分正在工作。但是當我改變手機上按鈕的音量時,屏幕上的搜索欄保持不變。 問題:當我增加/減少手機按鈕的音量時,SeekBar不會增加。 如何設置?Seek Bar由於點擊設備按鈕時音量沒有變化

import android.widget.SeekBar; 
import android.widget.SeekBar.OnSeekBarChangeListener; 
private SeekBar volumeSeekbar = null; 
private AudioManager audioManager = null; 

    private void initControls() 
    { 
     try 
     { 
      volumeSeekbar = (SeekBar)findViewById(R.id.seekBar1); 
      audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 
      volumeSeekbar.setMax(audioManager 
        .getStreamMaxVolume(AudioManager.STREAM_MUSIC)); 
      volumeSeekbar.setProgress(audioManager 
        .getStreamVolume(AudioManager.STREAM_MUSIC)); 


      volumeSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() 
      { 
       @Override 
       public void onStopTrackingTouch(SeekBar arg0) 
       { 
       } 

       @Override 
       public void onStartTrackingTouch(SeekBar arg0) 
       { 
       } 

       @Override 
       public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) 
       { 
        audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 
          progress, 0); 
       } 
      }); 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 
+0

http://stackoverflow.com/questions/10134338/using-seekbar -to-control-volume-in-android – AndRSoid

+0

與我的代碼相同,點擊硬件按鈕後不會改變 –

回答

0
private void initControls() { 
    // Return the handle to a system-level service - 'AUDIO'. 
    audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 

    // Find the seekbar 1 
    mediaVlmSeekBar = (SeekBar) findViewById(R.id.seekBar1); 
    // Set the max range(Volume in this case) of seekbar 
    // for Media player volume 
    mediaVlmSeekBar.setMax(audioManager 
      .getStreamMaxVolume(AudioManager.STREAM_MUSIC)); 
    // Set the progress with current Media Volume 
    mediaVlmSeekBar.setProgress(audioManager 
      .getStreamVolume(AudioManager.STREAM_MUSIC)); 

    try { 
     // Listener to receive changes to the SeekBar1's progress level 
     mediaVlmSeekBar 
       .setOnSeekBarChangeListener(new OnSeekBarChangeListener() { 
        public void onStopTrackingTouch(SeekBar arg0) { 
        } 

        public void onStartTrackingTouch(SeekBar arg0) { 
        } 

        // When progress level of seekbar1 is changed 
        public void onProgressChanged(SeekBar arg0, 
          int progress, boolean arg2) { 
         audioManager.setStreamVolume(
           AudioManager.STREAM_MUSIC, progress, 0); 
        } 
       }); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

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