java - 如何禁用声音

标签 java android

我正在制作一款休闲游戏。我在单击按钮等时应用了声音。现在我有一个扬声器图标的图像按钮。我想在按下时禁用声音。我怎么做?

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/

相关文章:

java - 如何调用不带参数的泛型方法?

java - Spring 启动: How to inject a Repository

android - 如何以编程方式将子菜单项添加到新的 Material 设计 android 支持库

android - 改造缓存数据以供离线使用

android - 缺少 Facebook Android SDK Audience Network NativeAdScrollView.CustomAdView

java - 为什么 cURL 不将 cookie 的过期/最大年龄值发送回服务器?

java - Android 正则表达式不符合我的预期

java - jax-rs 和服务器名称

Android - 如何从 map View 中获取 map 坐标

android - phonegap 地理定位不起作用