drcarter의 DevLog


음악을 스트리밍 하다가..볼륨을 조절 할 일이 생기게 되었을 경우.
안드로이드에 볼륨 조절하는 버튼이 있지만...
스트리밍 중에 화면에서 SeekBar를 통해서 조절을 하는 방법도 있다.
AudioManager 클래스에 있는 메소드를 이용하면 가능.

1. 볼륨 조절 버튼으로 조절 하기.

public boolean onKeyDown(int keyCode, KeyEvent event) {                  
        AudioManager mAudioManager = 
            (AudioManager)getSystemService(AUDIO_SERVICE);
        switch (keyCode) {
        case KeyEvent.KEYCODE_VOLUME_UP :
            mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                                             AudioManager.ADJUST_RAISE, 
                                             AudioManager.FLAG_SHOW_UI);
                return true;
        case KeyEvent.KEYCODE_VOLUME_DOWN:
            mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, 
                                             AudioManager.ADJUST_LOWER, 
                                             AudioManager.FLAG_SHOW_UI);
                return true;
        case KeyEvent.KEYCODE_BACK:
            return true;
        }

        return false;
   }

    public boolean onKeyUp(int keyCode, KeyEvent event) {
        AudioManager mAudioManager = 
           (AudioManager)getSystemService(AUDIO_SERVICE);
        switch (keyCode) {
        case KeyEvent.KEYCODE_VOLUME_UP :
            mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, 
                                             AudioManager.ADJUST_SAME, 
                                             AudioManager.FLAG_SHOW_UI);
                return true;
        case KeyEvent.KEYCODE_VOLUME_DOWN:
            mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, 
                                             AudioManager.ADJUST_SAME, 
                                             AudioManager.FLAG_SHOW_UI);
                return true;
        case KeyEvent.KEYCODE_BACK:
            this.finish();
            return true;
        }
        return false;
    }
2. 화면에서 SeekBar를 통해서 조절하기
seekVolumn = (SeekBar) findViewById(R.id.SeekBar_Volumn);
		final AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
		int nMax = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
		int nCurrentVolumn = audioManager
				.getStreamVolume(AudioManager.STREAM_MUSIC);

		seekVolumn.setMax(nMax);
		seekVolumn.setProgress(nCurrentVolumn);

		seekVolumn.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// TODO Auto-generated method stub
				audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
						progress, 0);
			}
		});