当我单击按钮设置音量0时,它可以正常工作。但是当我再次点击设置第75卷时,它无效。
btn_sound=(Button)findViewById(R.id.btn_sound);
btn_sound.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isMute){
isMute=false;
Music.setPlayerVolume(75, 75);
btn_sound.setBackgroundResource(R.drawable.sound_normal);
}else {
isMute=true;
Music.setPlayerVolume(0, 0);
btn_sound.setBackgroundResource(R.drawable.sound_silent);
}
}
});
Music.java类:
public class Music {
static MediaPlayer player=null;
public static MediaPlayer getPlayer(Activity activity){
if(player==null){
player = MediaPlayer.create(activity, R.raw.bac_music);
}
player.setLooping(true);
player.setVolume(75, 75);
return player;
}
public static void setPlayerVolume(float a,float b){
player.setVolume(a,b);
}
}
答案 0 :(得分:1)
MediaPlayer.setVolume(float, float)将浮点值从0f变为1f,你应该像这样调用它:
player.setVolume(0.75f, 0.75f);