设置视频音量 VideoView + SeekBar 为控制条

Set volume of a video of a VideoView + SeekBar as control bar

我必须设置 VideoView 中包含的视频的音量。我必须使用 SeekBar(我已经知道如何使用 SeekBars,我只是想知道如何设置视频的音量)。 我还必须使用另一个 SeekBar 作为控制栏。 这是我的代码:

    positionBar = (SeekBar) this.findViewById(R.id.seekBar2);
    volumeBar = (SeekBar) this.findViewById(R.id.seekBar);

    //getting the max volume and the current volume to set the volume bar
    audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
    maxVolume = audioManager.getStreamMaxVolume(AudioManager.USE_DEFAULT_STREAM_TYPE);
    currentVolume = audioManager.getStreamVolume(AudioManager.USE_DEFAULT_STREAM_TYPE);
    volumeBar.setMax(maxVolume);
    volumeBar.setProgress(currentVolume);

    //putting the video to execute in the video view
    videoPlayer.setVideoPath("android.resource://" + getPackageName() + "/" + R.raw.video);

    //volume bar listener
    volumeBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        @Override
        public void onProgressChanged(SeekBar seekBar, int i, boolean b) {


        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });
    //********************************************************************

    //position bar listener
    positionBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        @Override
        public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });
    //********************************************************************

    //timer to update the position bar during the video execution
    //and the volume
    new Timer().scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {

            currentVolume = audioManager.getStreamVolume(AudioManager.USE_DEFAULT_STREAM_TYPE);
            volumeBar.setProgress(currentVolume);
        }
    }, 0 , 500);
    //********************************************************************

使用 SeekBar.getProgress() 获取搜索栏的值,然后在 onProgressChanged 方法中使用该值设置音量。

检查下面的代码,以编程方式设置视频的音量。

SeekBar volumeBar;
int volumeLevel;
AudioManager audioManager;
ImageView volumnBtn;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    .....

    .....

    volumeBar = (SeekBar) findViewById(R.id.volumeBar);
    audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
    volumeBar.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
    volumeLevel = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);

    volumnBtn = (ImageView) findViewById(R.id.volumnBtn);

    if (volumeLevel == 0) {
        volumnBtn.setImageDrawable(getResources().getDrawable(R.drawable.volumnMute));
    } else {
        volumnBtn.setImageDrawable(getResources().getDrawable(R.drawable.volumnHigh));
    }

    iv_volumn_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            volumeLevel = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
            volumeBar.setProgress(volumeLevel);
            if (volumeBar.getVisibility() == View.GONE) {
                volumeBar.setVisibility(View.VISIBLE);
            } else {
                volumeBar.setVisibility(View.GONE);
            }
        }
    });

    volumeBar.setProgress(volumeLevel);
    volumeBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
            volumeLevel = i;
            if (volumeLevel == 0) {
                volumnBtn.setImageDrawable(getResources().getDrawable(R.drawable.volumnMute));
            } else {
                volumnBtn.setImageDrawable(getResources().getDrawable(R.drawable.volumnHigh));
            }
            audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, volumeLevel, 0);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            volumeBar.setVisibility(View.GONE);

            if (volumeLevel == 0) {
                volumnBtn.setImageDrawable(getResources().getDrawable(R.drawable.volumnMute));
            } else {
                volumnBtn.setImageDrawable(getResources().getDrawable(R.drawable.volumnHigh));
            }
        }
    });
}