2012-05-18 172 views
0

這是我的代碼,用於控制兩個seekbars在媒體播放器中爲兩個同時播放的聲音設置音量控制。但是音軌在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); 
      } 

      } 
     }); 
+0

嘗試設置您的mediaplayer音量,如: - 'mediaPlayer.setVolume((progress/100.0f),(progress/100.0f));'。我不確定,但你可以做一些像這樣的希望。 – Deepak

+0

您好Deepak您的帖子對我有幫助,但它有創建另一個問題時,我減少搜索欄然後音量正在減少,但進展增加它不是增加量在實際狀態...我不明白這種情況發生.. –

回答

0

我認爲mediaplayer.setVolume()希望有0.0F和1.0F之間的值,因此其相對捲到卷audiomanagers

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

這對我很有用。希望這也適用於你。