title: Too much work on main thread
body: <p>I am creating an application that plays back short media file. I want only one media button- when the file is being played it should show the <strong>stop media</strong> icon. As soon as the file is done playing it should revert back to the <strong>play</strong> icon. </p>
  
  <p>Note- <code>player</code> is the mediaplayer class with all the relevant methods (prepare; play; stop; pause etc)</p>
  
  <p>I try starting a separate thread in the <code>onCreate</code> of the activity like this:</p>
  
  <pre><code>Thread thread = new Thread()
  {
      @Override
      public void run()
      {
          final ImageButton button = (ImageButton)findViewById(R.id.lesson_media_button);
          //BUTTON
          button.setOnClickListener(new View.OnClickListener() 
          {   
              @Override
              public void onClick(View v) 
              {
                  if(media_button == true)
                  {
                      media_button = false;
                      player.PlaySound();
                      button.setImageResource(R.drawable.stop);
                  }
                  else if(media_button == false)
                  {
                      media_button = true;
                      player.StopSound(mycontext, audio);
                      button.setImageResource(R.drawable.play);
                  }
  
                  while(true)
                  {
                      if(player.isPlaying() == false)
                      {
                          media_button = true;
                          button.setImageResource(R.drawable.play);
                          break;
                      }
                  }
              }
          });
  
      }
  };
  thread.start();
  </code></pre>
  
  <p>The playback is working fine, however I can never stop the file when it is busy playing and the image always shows the <strong>play</strong> icon (never <strong>stop</strong>). When the file is done playing I get <strong>the application may be doing too much work on its main thread</strong> warning.</p>
  
  <p>Can anyone help or suggest a better way to go about this?
  Cheers</p>