如何使声音池静音? [英] How to mute a soundpool?

查看:191
本文介绍了如何使声音池静音?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想允许用户使用按钮使应用程序静音,有人建议我在按下静音按钮时在您设置为false的MediaPlayerPool上粘贴一个布尔值".然后在你的playSound方法中,如果值是false则什么也不做.''但是我不知道该怎么做.有人可以张贴示例代码pls. 池代码:

I want to allow the user to mute the app with a button and someone advised me to ''stick a boolean on your MediaPlayerPool that you set to false when the mute button is pressed. Then in your playSound method, do nothing if the value is false.''but i dont know how to do that. Could someone post an example code pls. The pool code :

public class MediaPlayerPool {

    private static MediaPlayerPool instance = null;
    private Context context;
    private List<MediaPlayer> pool;

    public static MediaPlayerPool getInstance(Context context) {
        if(instance == null) {
            instance = new MediaPlayerPool(context);
        }
        return instance;
    }

    private MediaPlayerPool(Context context) {
        this.context = context;
        pool = new ArrayList<>();
    }

    public void playSound(int soundId) {
        MediaPlayer mediaPlayer = MediaPlayer.create(context, soundId);

        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                mediaPlayer.release();

                pool.remove(mediaPlayer);
                mediaPlayer = null;

            }
        });

        pool.add(mediaPlayer);
        mediaPlayer.start();
    }

}

推荐答案

向您的MediaPlayerPool类添加变量,我们将其称为mute

Add a variable to your MediaPlayerPool class, let's call it mute

public boolean mute = false;

具有一个静音/取消静音按钮,并在其onClick方法上(将切换):

Have a mute/unmute button and on its onClick method (will toggle):

MediaPlayerPool.getInstance().mute = !MediaPlayerPool.getInstance().mute

,您的playSound方法变为

public void playSound(int soundId) {
    if(!mute) {
        // stick your current code here
    }
}

这篇关于如何使声音池静音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆