Android如何知道按下的按钮是否正在播放随机声音

时间:2013-12-28 14:33:17

标签: android random case

我想制作一个儿童游戏,我有各种按钮,每个都有声音​​,我想只有一个按钮播放随机声音,孩子们必须按右动物按钮才能得到“正确”的消息然后再次点击随机声音再次播放。

所以,随机播放我有这个工作:

public class MainActivity extends Activity {
    Button cao,vaca;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final MediaPlayer sound1 = MediaPlayer.create(this, R.raw.cao);
        final MediaPlayer sound2 = MediaPlayer.create(this, R.raw.gato);
        final MediaPlayer sound3 = MediaPlayer.create(this, R.raw.galinha);

        cao = (Button) findViewById(R.id.cao);
        vaca = (Button) findViewById(R.id.vaca);


        cao.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                 Random randomGenerator = new Random();
                    int randomInt = randomGenerator.nextInt(3) + 1;

                    //picking the right sound to play
                    switch (randomInt){
                    case 1: sound1.start(); 

                    break;
                    case 2: sound2.start();
                    break;
                    case 3: sound3.start();
                    break;
                    }


            }
        });





    }

现在例如我如何按下按钮“vaca”做某事,否则在案例中随意做些什么?

Random randomGenerator = new Random();
                    int randomInt = randomGenerator.nextInt(3) + 1;

                    //picking the right sound to play
                    switch (randomInt){
                    case 1: sound1.start(); 
                   // if button vaca pressed ....
                   // else....

                    break;
                    case 2: sound2.start();
                    break;
                    case 3: sound3.start();
                    break;
                    }

1 个答案:

答案 0 :(得分:0)

代码和对象太多了。

public class MainActivity extends Activity {
Button cao,vaca;
private MediaPlayer player;

...

@Override
    protected void onCreate(Bundle savedInstanceState) {
        player = new MediaPlayer();

...

        @Override
        public void onClick(View v) {
            Resources resources = getResources();

             Random randomGenerator = new Random();
                int randomInt = randomGenerator.nextInt(3) + 1;
                if(player.isPlaying()) player.stop();
                //picking the right sound to play
                switch (randomInt){
                case 1: 
                   player.setDataSource(resources.openRawResource(R.raw.cao));
                break;
                case 2: player.setDataSource(resources.openRawResource(R.raw.gato));
                break;
                case 3: player.setDataSource(resources.openRawResource(R.raw.gatalina));
                break;
                }
                player.prepare();
                player.start();

        }
    });