Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid - Pause execution until sound has played
    primarykey
    data
    text
    <p>I'm creating a Simon Says app for Android. The game has four tiles: red, blue, green, and yellow. When the game starts, the program picks a random number between 1 and 4 (which corresponds to the four colors) and stores it in a string. Each round of play adds a new digit to the string. The program then should then the first character in the string and play a unique sound and brighten the color of the appropriate button for ~1 second before moving on to the next character. Each time the player correctly inputs the pattern, it adds another digit to the string.</p> <p>My problem is this: I have found methods that keep the button brightened for the duration of the sound, but all of them cause the program to execute the entire for loop in the amount of time it takes for the sound to finish playing. Here is my code:</p> <pre><code>case R.id.new_game_button: if (event.getAction() == MotionEvent.ACTION_DOWN) { combination += randomGenerator(); for (int i=0; i &lt; combination.length(); i++) { if (combination.charAt(i) == 49) { findViewById(R.id.red_button).setBackgroundResource(color.red_pressed); red_sound.start(); red_sound.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { findViewById(R.id.red_button).setBackgroundResource(color.red); } }); } } } break; </code></pre> <p>I've tried Thread.sleep(), OnCompletionListeners, postDelayed Handlers, and CountDownTimers, but I can't get any of them to do what I need them to do. How might I go about halting the for loop from executing while the sound is playing?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload