본문 바로가기

안드로이드

Seekbar 볼륨 조절 AlertDialog 이용

Button btn_vol = (Button) findViewById(R.id.btn_vol);

btn_vol.setOnClickListener(new View.OnClickListener() { //볼륨설정 , 

@Override

public void onClick(View v) {

View innerView = getLayoutInflater().inflate(R.layout.seek_bar, null);

AlertDialog.Builder adialog = new AlertDialog.Builder(

PlayListActivity.this);

adialog.setView(innerView);

seekVolumn = (SeekBar) innerView.findViewById(R.id.SeekBar_Volumn);

setSeekbar();


AlertDialog alert = adialog.create();

alert.setTitle("볼륨설정");

alert.show();

}

});



private void setSeekbar(){

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

            }

        });

}



seek_bar.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/container"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:gravity="center"

    android:orientation="vertical"

    android:padding="30dip" >


    <SeekBar

        android:id="@+id/SeekBar_Volumn"

        android:layout_width="200dip"

        android:layout_height="wrap_content" />


</LinearLayout>