验证android中的随机音符

时间:2017-09-11 11:55:01

标签: android verify

我有一个选择并播放随机音符的数组。我想要做的是当你双击钢琴键时,代码应该检查并查看它是否与随机选择的音符相同,是什么想法?

以下是代码:

package com.stefanawesome.piano;

import android.content.pm.ActivityInfo; import
android.media.AudioManager; import android.media.MediaPlayer; import
android.media.SoundPool; import android.os.Build; import
android.provider.ContactsContract; import
android.support.v7.app.AppCompatActivity; import android.os.Bundle;
import android.view.View; import android.widget.Button; import
android.widget.ImageButton; import android.widget.TextView;

import java.util.ArrayList; import java.util.List; import
java.util.Random;


public class MainActivity extends AppCompatActivity {

    //Declaring variables and array:
    ImageButton NoteNew;
    ImageButton Repeat;
    Button c, d, e, f, g, a, b, high;
    List<Integer> soundList = new ArrayList<Integer>();
    private SoundPool soundpool;
    private int sound_c, sound_d, sound_e, sound_f, sound_g, sound_a, sound_b, sound_high;

    //Random Note Function:
    int last;

    private void playRandomSound() {
        int randomInt = (new Random().nextInt(soundList.size()));
        last=randomInt;
        int rSound = soundList.get(randomInt);
        MediaPlayer mp = MediaPlayer.create(this, rSound);
        mp.start();
    }

    //Repeat Note
    private void playlastRandomSound() {
        int rSound = soundList.get(last);
        MediaPlayer mp = MediaPlayer.create(this, rSound);
        mp.start();
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        //Find View by ID all here:
        soundList.add(R.raw.c);
        soundList.add(R.raw.d);
        soundList.add(R.raw.e);
        soundList.add(R.raw.f);
        soundList.add(R.raw.g);
        soundList.add(R.raw.a);
        soundList.add(R.raw.b);
        soundList.add(R.raw.chigh);
        NoteNew = (ImageButton) findViewById(R.id.Newnote);
        Repeat = (ImageButton) findViewById(R.id.Repeat);
        c = (Button) findViewById(R.id.C);
        d = (Button) findViewById(R.id.D);
        e = (Button) findViewById(R.id.E);
        f = (Button) findViewById(R.id.F);
        g = (Button) findViewById(R.id.G);
        a = (Button) findViewById(R.id.A);
        b = (Button) findViewById(R.id.B);
        high = (Button) findViewById(R.id.high);


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            soundpool = new SoundPool.Builder().setMaxStreams(5).build();
        } else {
            soundpool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);

        }

        //Soundpools over here:
        sound_c = soundpool.load(this, R.raw.c, 1);
        sound_d = soundpool.load(this, R.raw.d, 1);
        sound_e = soundpool.load(this, R.raw.e, 1);
        sound_f = soundpool.load(this, R.raw.f, 1);
        sound_g = soundpool.load(this, R.raw.g, 1);
        sound_a = soundpool.load(this, R.raw.a, 1);
        sound_b = soundpool.load(this, R.raw.b, 1);
        sound_high = soundpool.load(this, R.raw.chigh, 1);


        //When button gets clicked do something:
        c.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                soundpool.play(sound_c, 1, 1, 0, 0, 1);
            }
        });

        d.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                soundpool.play(sound_d, 1, 1, 0, 0, 1);
            }
        });

        e.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                soundpool.play(sound_e, 1, 1, 0, 0, 1);
            }
        });

        f.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                soundpool.play(sound_f, 1, 1, 0, 0, 1);
            }
        });

        g.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                soundpool.play(sound_g, 1, 1, 0, 0, 1);
            }
        });

        a.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                soundpool.play(sound_a, 1, 1, 0, 0, 1);
            }
        });

        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                soundpool.play(sound_b, 1, 1, 0, 0, 1);
            }
        });

        high.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                soundpool.play(sound_high, 1, 1, 0, 0, 1);
            }
        });
        NoteNew.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                playRandomSound();
            }
        });

        Repeat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                playlastRandomSound();
            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

 c.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        soundpool.play(sound_c, 1, 1, 0, 0, 1);
        if(last == 0){
           "You played the last note."
        }
    }
});
  d.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        soundpool.play(sound_d, 1, 1, 0, 0, 1);
        if(last == 1){
           "You played the last note."
        }
    }
});

由于听众和列表的顺序相同,您可以这样做。一个更好的选择是使用声音和id创建另一个名为Sounds或类似的类。