验证 android 中的随机笔记
Verifying random note in android
我有一个数组可以选择并播放随机音符。我想要实现的是,当您双击钢琴键时,代码应该检查它是否与随机选择的音符相同,有什么想法吗?
代码如下:
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();
}
});
}
}
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."
}
}
});
由于监听器和列表的顺序相同,您可以这样做。更好的选择是创建另一个 class 名为 Sounds 或类似的东西,带有声音和 id。
我有一个数组可以选择并播放随机音符。我想要实现的是,当您双击钢琴键时,代码应该检查它是否与随机选择的音符相同,有什么想法吗?
代码如下:
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();
}
});
}
}
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."
}
}
});
由于监听器和列表的顺序相同,您可以这样做。更好的选择是创建另一个 class 名为 Sounds 或类似的东西,带有声音和 id。