首页 > 解决方案 > 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();
    }
}

所以我想知道我的错误在哪里以及为什么我的应用程序似乎不起作用。谢谢你们。

标签: android

解决方案


推荐阅读