如何将声音池静音?

时间:2018-02-26 09:45:51

标签: java android android-mediaplayer soundpool mute

我希望允许用户使用按钮将应用程序静音,有人建议我在按下静音按钮时将MediaPlayerPool上的布尔值设置为false。然后在你的playSound方法中,如果值为false,则不执行任何操作。''但我不知道该怎么做。有人可以发布示例代码。 池代码:

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();
    }

}

0 个答案:

没有答案