Android studio非常简单的鼓机
我正在尝试做一个非常基本的步进序列器鼓机。我的应用程序只有2个图像按钮(播放和停止)和4个切换按钮。该应用程序的目标是,当我按下播放按钮时,该应用程序为每个打开的切换按钮播放一个圈套声音,但不是一次全部播放,只是一个接一个地播放(就像一种步序器),并在循环中不断播放声音,直到我按下停止按钮 到目前为止,这是我的代码Android studio非常简单的鼓机,android,Android,我正在尝试做一个非常基本的步进序列器鼓机。我的应用程序只有2个图像按钮(播放和停止)和4个切换按钮。该应用程序的目标是,当我按下播放按钮时,该应用程序为每个打开的切换按钮播放一个圈套声音,但不是一次全部播放,只是一个接一个地播放(就像一种步序器),并在循环中不断播放声音,直到我按下停止按钮 到目前为止,这是我的代码 public class Sampler { SoundPool sp; private int snareId; private Context myco
public class Sampler {
SoundPool sp;
private int snareId;
private Context mycontext;
public boolean playing;
public ToggleButton button1, button2, button3, button4;
public ImageButton playButton, stopButton;
public Sampler(Context appcontext) {
this.mycontext = appcontext;
sp = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
snareId = sp.load(mycontext, R.raw.snare, 1);
}
public void play() {
if (playButton.isPressed()) {
playing = true;
}
while (playing == true) {
if (button1.isChecked()) {
sp.play(snareId, 10, 10, 1, 0, 1);
try {
Thread.sleep(60000 / (120 * 4));
} catch (InterruptedException e) {
e.printStackTrace();
}
if (button2.isChecked()) {
sp.play(snareId, 10, 10, 1, 0, 1);
try {
Thread.sleep(60000 / (120 * 4));
} catch (InterruptedException e) {
e.printStackTrace();
}
if (button3.isChecked()) {
sp.play(snareId, 10, 10, 1, 0, 1);
try {
Thread.sleep(60000 / (120 * 4));
} catch (InterruptedException e) {
e.printStackTrace();
}
if (button4.isChecked()) {
sp.play(snareId, 10, 10, 1, 0, 1);
try {
Thread.sleep(60000 / (120 * 4));
} catch (InterruptedException e) {
e.printStackTrace();
}
if(playing ==false){
break;
}
}
}
}
}
}
}
public void stop(){
if (stopButton.isPressed()) {
playing = false;
}
}
}
问题是,当我运行应用程序时,按下播放或停止按钮会使我的应用程序崩溃并退出,我不知道为什么。我还有一个非常基本的主要活动代码
public class MainActivity extends AppCompatActivity {
public ToggleButton button1, button2, button3, button4;
public Sampler mySample;
public ImageButton playButton;
public ImageButton stopButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playButton = (ImageButton) findViewById(R.id.play);
stopButton = (ImageButton) findViewById(R.id.stop);
button1 = (ToggleButton) findViewById(R.id.button1);
button2 = (ToggleButton) findViewById(R.id.button2);
button3 = (ToggleButton) findViewById(R.id.button3);
button4 = (ToggleButton) findViewById(R.id.button4);
}
public void playSeq() {
mySample.play();
}
public void stopSeq() {
mySample.stop();
}
}
所以我想知道我的错误在哪里,为什么我的应用程序似乎无法工作。谢谢大家。stacktrace怎么说?它说:“在android的父或祖先上下文中找不到方法playSeq(视图):在视图类androidx.appcompat.widget.AppCompatImageButton上定义的onClick属性,id为“play”这是否意味着我的播放按钮路径错误,或者我的方法错误?例如,意味着
public void playSeq()
需要更改为public void playSeq(视图v)
。可能还需要添加到stopSeq()
感谢您的回答。不幸的是,我试图按照您所说的进行更改,但仍然崩溃,但现在stacktrace上出现了一个新错误:“无法为android执行方法:onClick”