Sound plays earlier than view is changed

65 Views Asked by At

I am creating a piano app in android studio:

screenshot of app

I have an on click listener for my play button which when pressed is supposed to make the record and play buttons invisible and the stop button visible while the recorded sounds are playing.

Play button

mBtn_Play.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            mRecordingState = "Playing";
            switchButtonVisibility();

            for (final int sound: mListRecordedSounds )
            {
                if (mRecordingState == "Ready")
                {//break out of loop when stop button is pressed
                    break;
                }
                else {

                    mSoundPool.play(sound, 1,1,1,0,1);
                    try
                    {
                        Thread.sleep(500);
                    }
                    catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                }
            }
        }
    });
}

Method for switching button visibility

private void switchButtonVisibility()
{

    if (mRecordingState != "Ready")
    {
        mBtn_Stop.setVisibility(View.VISIBLE);
        mBtn_Record.setVisibility(View.GONE);
        mBtn_Play.setVisibility(View.GONE);

    }
    else
    {
        mBtn_Stop.setVisibility(View.GONE);
        mBtn_Record.setVisibility(View.VISIBLE);
        mBtn_Play.setVisibility(View.VISIBLE);
    }

}

With the record and stop buttons, this works correctly

mBtn_Record.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            mListRecordedSounds.clear();
            mRecordingState = "Recording";
            switchButtonVisibility();

        }
    });
mBtn_Stop.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            mRecordingState = "Ready";
            switchButtonVisibility();
        }
    });

For some strange reason it is executing the for loop first and playing the sounds before the switchButtonVisibility(); method changes which buttons are visible. This does not seem to make any sense as the method is above the loop. Is there any way to set it so the button visibility is changed first before the loop is executed and the sounds are played?

1

There are 1 best solutions below

2
On BEST ANSWER

Delay the execution, posting an event on the view. This will ensure, that the action would be performed as soon as the view is updated:


    @Override
    public void onClick(View v) {
        mRecordingState = "Playing";
        switchButtonVisibility();

        mBtn_Play.post(new Runnable() {
            @Override
            public void run() {
                for (final int sound : mListRecordedSounds ) {
                    // play the sound here
                    ...
                }
            }
        });
    }