使用搜索栏设置媒体播放器对象的音量

时间:2014-01-08 07:14:56

标签: android audio volume seekbar

我想在搜索栏的帮助下设置我的媒体播放器对象的音量。我在我的应用程序中使用了它的setVolume()方法,但没有用,因为音量没有变化。 这是我的代码:

    setVolumeControlStream(AudioManager.STREAM_MUSIC);
        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) {
                // TODO Auto-generated method stub
                Log.i(tag,"touch stopped");

            }

            @Override
            public void onStartTrackingTouch(SeekBar arg0) {
                // TODO Auto-generated method stub
                Log.i(tag,"touch started");

            }
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                //mp is the mediaplayer object..
                mp.setVolume(progress, progress);
                Log.i(tag,"working with seekbar");
                            }

        });
    }  

audioManager和volumeseekbar已初始化。

1 个答案:

答案 0 :(得分:3)

使用它为我工作的下面代码..

private SeekBar mSeekBarVolume;
    private AudioManager audioManager;
    private int maxVolume;
    private int curVolume;
/**
     * To initialize the seek bar
     */
    private void initialiseSeekBar() {

        maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        mSeekBarVolume.setMax(maxVolume);
        mSeekBarVolume.setProgress(curVolume);
        mSeekBarVolume
                .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);
                    }
                });
    }