我正在制作一款休闲游戏。我在单击按钮等时应用了声音。现在我有一个扬声器图标的图像按钮。我想在按下时禁用声音。我怎么做?
I used sound pool here is my sound loop class:
public class SoundPool {
private static SoundPool soundpool;
private static int clicksound;
private static int gameosound;
private static int scoreupsound;
public SoundPlayer (Context context) {
soundpool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
clicksound = soundpool.load(context,R.raw.click, 1);
gameosound= soundpool.load(context,R.raw.gameover, 1);
scoreupsound = soundpool.load(context,R.raw.score, 1);
}
public void playclicksound() {
soundpool.play(clicksound, 1.0f, 1.0f, 1, 0, 1.0f);
}
public void playgameosound() {
soundpool.play(gameosound, 1.0f, 1.0f, 1, 0, 1.0f);
}
public void playscoreupsound() {
soundpool.play(scoreupsound, 1.0f, 1.0f, 1, 0, 1.0f);
}
}
public class MainActivity {
private TextView word1;
private ImageButton speakericon;/* I want to change image when pressed*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
word1 = (TextView) findViewById(R.id.word1);
word1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sound.playclicksound(); /* I want to cancel this sound when the speaker icon is pressed */
});
}
最佳答案
新建一个类并在里面添加:
soundpool.pause();
或
soundpool.stop();
像这样:
public void stopclicksound() {
soundpool.stop();
}
关于java - 如何禁用声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48968008/