title: How to highlight text while MediaPlayer is playing a sound in Android?
body: <p>I want to highlight the text in a TextView as a MediaPlayer plays the pre-recorded sound of the text. (like karaoke) For now, I just want to cover no text at the beginning of the sound and all the text at the end of the sound. (My highlighted text should look like a progress bar.)</p>
  
  <p>I tried <code>isPlaying()</code> method of MediaPlayer object, to get the index of text, I calculated the percentage.</p>
  
  <p>My code is below.</p>
  
  <pre><code>while(mediaPlayer.isPlaying())
  {
    int percentage = mediaPlayer.getCurrentPosition() / mediaPlayer.getDuration() * 100;
    int end = text.length() * percentage / 100;
    story.setText("Hello world!", BufferType.SPANNABLE);
    Spannable span = new SpannableString(story.getText());
    text = story.getText().toString();
    span.setSpan(new BackgroundColorSpan(0xFF00FF00), 0, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    story.setText(span);
  }
  </code></pre>
  
  <p>This seems to be blocking the thread because my app freezes. I tried using AsyncTask but failed.</p>
  
  <p>Do you know how do I make it work?</p>