媒体播放器错误

时间:2012-05-18 13:38:43

标签: android media-player android-audiomanager

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

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

            }
        });

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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);
    }
}

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