0

Its a metronome app, but this part works basically like a drum machine. I have a separate thread that is constantly writing bytes of sound and bytes of zeros to the AudioPlayer in a function I called play(). On my main thread I dynamically generate a few buttons that set which beat is accented and this is stored in an array that play() function is looping through and based on the value in the array its either playing accented sound, non-accented sound or silence.

This is what I have in main activity. The generateButton() function is just dynamically generating different numbers of buttons and its storing them in an array. Each time the user changes the number of buttons Im running this function to generate new n buttons. And every time user presses on a button Im updating the accentArray in metronome. Im using flexbox layout to space evenly the buttons.

flexboxLayout = (FlexboxLayout) findViewById(R.id.flexboxContainer);
generateButtons();

Ive made a class just for making threaded metronome instance. Like this:

public class MetronomeThread extends Thread{
    Metronome metronome;

    MetronomeThread(Context context, double bpm, int state[]){
        metronome = new Metronome(context, bpm, state);
    }

    @Override
    public void run() {
        metronome.loadSound();
        metronome.play();
    }

    public void end(){
        metronome.stop();
        metronome = null;
    }
    public void setBpm(double bpm){
        metronome.setBpm(bpm);
    }
    public void calcSilence(){
        metronome.calcSilence();
    }
    public void updateAccentArray(int[] array){
        metronome.updateAccentArray(array);
    }
}

And this is the function in Metronome class that loops through the accentArray:

 public void play(){
        int i = 0;
        do {
            switch (btnState[i]){
                case 0:
                    audioPlayer.writeToPlayer(fullSilenceArray);
                    break;
                case 1:
                    audioPlayer.writeToPlayer(tockSound);
                    audioPlayer.writeToPlayer(tockSilenceArray);
                    break;
                case 2:
                    audioPlayer.writeToPlayer(tickSound);
                    audioPlayer.writeToPlayer(tickSilenceArray);
                    break;
            }
            if(i < btnState.length - 1){
                i++;
            } else {
                i = 0;
            }
        } while (play);
        audioPlayer.destroyPlayer();
    }

Okay so what Im trying to do is each time I write to the player I want the correct button to flash to visually show on which beat the metronome is. Im not sure how to do this. Im pretty new to the android. Somebody please help.

Dangz1
  • 3
  • 1
  • 1
    Android thread basics answer this at https://stackoverflow.com/a/12850190/2373819 so use `runOnUiThread()` to flash the button as ui updates can only be done from the UI thread. – Andrew Jan 31 '21 at 22:18
  • @Andrew Ok so if I understand correctly I should use `runOnUiThread()` from metronomeThread? Do I then pass the view where the buttons are to the Metronome class? Or am I not understanding this correctly. – Dangz1 Jan 31 '21 at 23:21

0 Answers0