title: Android multi-threading - Timing of playing sounds & responses
body: <p>Really new to this stuff so bear with me!</p>
  
  <p>In my app I need to prompt the user to respond via button press to a noise, and record the response if it happens only while the sound is playing. I also need to have a variable sound presentation time, and variable time between the sounds...</p>
  
  <p>The issue that I am facing is that I need a way to issue a delay between the presentations of the sounds, and have a delay between a sound play and sound stop command. I understand that to use a wait() I need to use another thread so that I don't pause the UI thread.</p>
  
  <p>I need to some help as to which is the most elegant way of doing this? At the moment I have this:</p>
  
  <pre><code> public void onClick(View v) {
  
      switch (v.getId()){
  
          case R.id.start:
  
  
              presentTone(1000,60,2000,"l");
              // I need to insert a delay here, or in the presentTone method body 
              break;
  </code></pre>
  
  <p>and my presentTone method is this: </p>
  
  <pre><code>private void presentTone(int frequency,int amplitude,final int length,String side){
  
  
  // GETTING THE CORRECT AMPLITUDE LEVEL
  
      // Find the correction level as a float
      correctionDB = calibrationOffsets.get(frequency + "L");
  
      // Decibels for 5 dB drops
      volLog = (float) (Math.pow(10,((amplitude-100)/20)));
  
      // Multiply the decibels correction with the presentation level to get the calibrated level if the offset is positive
      volLog = (float) (volLog*(Math.pow(10,((correctionDB)/20))));
  
      // Check to see if the system is capable of the 80 dB desired level
      if (volLog&gt;1){
          Toast.makeText(this, "Over Error - System is not capable of this amplitude level!" ,Toast.LENGTH_LONG).show();
      }
  
  
  //PICKING THE CORRECT HEADPHONE SIDE TO PLAY
  
      volLogL = volLog;
      volLogR = volLog;
  
      if (side.toLowerCase().contains("l")){
          volLogR = (float) 0.0;
      }else if (side.toLowerCase().contains("r")){
          volLogL = (float) 0.0;
      }
  
  //PLAYING THE TONE
  
      // Playing the tone
      mp.reset();
      mp = MediaPlayer.create(this,idReferences.get(freq_selection));
      mp.setVolume(volLogL, volLogR);
      mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
          @Override
          public void onPrepared(MediaPlayer mp) {
              if (mp.isPlaying()) {
                  mp.stop();
                  mp.reset();
              }
              try {
                  mp.start();{
  
  
  //* I need to add a delay here, so I think I need to run the whole "Playing the tone" section in another thread?
  
  
  
                  }
              } catch (IllegalStateException e) {
                  e.printStackTrace();
              }
          }
      });
  
  }
  </code></pre>
  
  <p>I need the new thread to be within the presentTone method, so that I can start the playback, delay for a time, then stop. I also need to feed back a boolean for whether the sound is playing so that I can handle user response.</p>
  
  <p>Apologies if what I am asking is unclear, I'm doing my best to make it obvious! I'd appreciate any kind of help. Thanks</p>