Search code examples
androidverify

Verifying random note in android


I have an array that chooses and plays a random note. What I want to make happen is when you double click a piano key the code should check and see if it's the same note as the randomly chosen one, any ideas?

Here's the code:

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

Solution

  •  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."
            }
        }
    });
    

    Since the listeners and the list got the same order, you can do this. A better choice is creating another class called Sounds or something like that, with the sound and an id.