More complete android MP3 LRC lyrics scroll to highlight of with source code

  • 2020-05-19 05:46:53
  • OfStack

1. In the past, scrolling was only used to refresh rows, but now it is no longer used to scroll by rows. In fact, it is used to move upward as a whole within a certain period of time.

The key codes are as follows:


        float plus = currentDunringTime == 0 ? 30
                : 30
                        + (((float) currentTime - (float) sentenctTime) / (float) currentDunringTime)
                        * (float) 30;
        //  Scroll up   This is a scroll based on the length of the lyrics, moving the whole thing up 
        canvas.translate(0, -plus);

plus is the size of each movement, which is calculated according to the duration of the lyrics. The longer the time, the smaller the value of plus. See the specific code.

Here's how to get the lyrics:


 public void updateIndex(long time) {
  this.currentTime = time;
  //  The lyrics serial number 
  index = mLyric.getNowSentenceIndex(time);
  if (index != -1) {
   Sentence sen = Sentencelist.get(index);
   sentenctTime = sen.getFromTime();
   currentDunringTime = sen.getDuring();
  }
 }

Here's how to follow the new thread:

 class UIUpdateThread implements Runnable {
  long time = 100; //  The scrolling speed 
  public void run() {
   while (mp.isPlaying()) {
    lyricView.updateIndex(mp.getCurrentPosition());
    mHandler.post(mUpdateResults);
    try {
     Thread.sleep(time);
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  }
 }

project link: click to download


Related articles: