我正在创造一款休闲游戏。当按下按钮等时我应用了声音。现在我有一个扬声器图标的图像按钮。我想在按下时禁用声音。我怎么做?
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 */
});
}
答案 0 :(得分:0)
创建一个新类并添加到内部:
soundpool.pause();
或
soundpool.stop();
这样的事情:
public void stopclicksound() {
soundpool.stop();
}