android - Android studio 非常简单的鼓机
问题描述
我正在尝试做一个非常基本的步进音序器鼓机。我的应用程序只有 2 个图像按钮(播放和停止)和 4 个切换按钮。该应用程序的目标是,当我按下播放按钮时,该应用程序会为每个打开的切换按钮播放一个军鼓声音,但不是一次全部打开,只是一个接一个地播放(就像一种步进音序器)并继续播放声音循环直到我按下停止按钮。
到目前为止,这是我编码的
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();
}
}
所以我想知道我的错误在哪里以及为什么我的应用程序似乎不起作用。谢谢你们。
解决方案
推荐阅读
- r - GGplot2 条形图 - 将两个 y 值映射到 1 个 x 值
- typescript - 使用先进的前沿算法填充网格孔
- python - 为列表的变量名称赋值
- html2pdf - 为什么用阿拉伯语文本 html2pdf 删除句子最后一个单词之前的最后一个空格?
- javascript - Ionic-4 ion-alert-controller 创建功能不起作用
- c# - 自定义组件在设计器中工作,但在代码中不可见
- c - C语言:带字符串的二叉搜索树
- .net - 为什么在水晶报告中扫描二维码时无法打开网站网址?
- java - 仅在 IntelliJ 中测试失败,但适用于 Maven 命令行
- reactjs - 这两个 reactjs setState 行为之间的区别