如何使随机声音时,点击链接? [英] How to make Random sound when button click?

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

问题描述

所以,我在这里有4个声,我是用的Soundpool

So i have 4 sound here, i was use SoundPool

     sound1 = soundPool.load(this, R.raw.aww, 1);
     sound2 = soundPool.load(this, R.raw.arh, 1);
     sound3 = soundPool.load(this, R.raw.agg, 1);
     sound4 = soundPool.load(this, R.raw.uhh, 1);

所以我不知道如何使按钮选择随机声:

so i wonder how to make button choose random sound :

    click= (Button)findViewById(R.id.bm);
    click.setOnClickListener(new View.OnClickListener() {

          public void onClick(View click){             
             //choose one of four sound to play   
            }
        });
}

任何人有一些想法?

Anyone have some Idea?

推荐答案

您可以存储soundIDs在一个阵列和Random类的Java选择其中一个随机。

You can store soundIDs in an array and select one of them randomly with Random class of Java.

int[] sound = new int[4];
sound[0] = soundPool.load(this, R.raw.aww, 1);
sound[1] = soundPool.load(this, R.raw.arh, 1);
sound[2] = soundPool.load(this, R.raw.agg, 1);
sound[3] = soundPool.load(this, R.raw.uhh, 1);

Random random = new Random();

click = (Button) findViewById(R.id.bm);
click.setOnClickListener(new View.OnClickListener() {
    public void onClick(View click) {
        //choose one of four sound to play
        soundPool.play(sound[random.nextInt(4)], 1.0f, 1.0f, 0, 0, 1.0f);
    }
});

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

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