0

这是我用于控制两个搜索栏以在媒体播放器中设置音量控制以同时播放两个声音的代码。但是在seekbar的进度变化时声音会打开和关闭,在进度变化时不会频繁增加或减少音量。我哪里错了?

audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

        int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        int curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);

        volume2 = (SeekBar)findViewById(R.id.volbar2);
        volume1 = (SeekBar)findViewById(R.id.volbar);


        volume1.setMax(maxVolume);
        volume2.setMax(maxVolume);
        volume1.setProgress(curVolume);
        volume2.setProgress(curVolume);

        volume1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {


               }
            @Override
            public void onStartTrackingTouch(SeekBar arg0) {

            }
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean arg2) {

                if(seekBar.equals(volume1)){

                    mediaPlayer.setVolume(progress,progress);

                    Toast.makeText(getApplicationContext(), ""+progress+progress, 1).show();

                      }

            }
            });

        volume2.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

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

            }

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

            }

            public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
                // TODO Auto-generated method stub
                if(seekBar.equals(volume2)){ 
                     mediaPlayer2.setVolume(progress,progress);
             }

            }
        });
4

2 回答 2

0

我认为 mediaplayer.setVolume() 希望值介于 0.0f 和 1.0f 之间,因此它是相对于 audiomanagers 音量的音量

于 2012-05-18T13:50:41.597 回答
0

@Ankush嗨,实际上我使用以下代码为我的项目解决了这个问题:

public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    Log.w("MovingProgressBar","Progress change to: " + progress);
    try {
        if (seekBar == seekMusic){
            musicCurrentVol = (float) (progress/100.0);
            musicPlayer.setVolume(musicCurrentVol, musicCurrentVol);
        } else if (seekBar == seekIns) {
            insCurrentVol = (float) (progress/100.0);
            insPlayer.setVolume(insCurrentVol, insCurrentVol);
        } 
    } catch (Exception e) {
        Log.w("MovingProgressBar:", "Progress not change: " + e);
    }
}

这对我有用。希望这也对你有用。

于 2012-05-21T10:57:02.287 回答