Android Implements lyrics sync display when song is played

Source: Internet
Author: User
Tags stub thread trim

We need to read each line of the lyrics file above to convert it into a lyric entity:

public class Lyricobject {public   

 
    int begintime;//start time public    
    int endtime;//end time public    
    int timeline;//Single When the lyrics are public    
    String LRC;//simple sentence lyrics    
}

According to the current player's playback progress and the start time of each line of lyrics, the current screen highlighted in the center of the sentence lyrics. In the UI thread, another thread is OnDraw () to redraw the screen every 100ms to achieve the animation effect of the lyrics smoothing scrolling. The mainactivity code is as follows:

Import java.io.IOException;    
Import android.app.Activity;    
Import Android.media.MediaPlayer;    
Import Android.net.Uri;    
Import Android.os.Bundle;    
Import android.os.Environment;    
Import Android.os.Handler;    
Import Android.view.View;    
Import Android.view.View.OnClickListener;    
Import Android.widget.Button;    
Import Android.widget.SeekBar;    
        
Import Android.widget.SeekBar.OnSeekBarChangeListener; The public class Mainactivity extends activity {/** called the ' when the ' is the ' is ' the ' activity ' is a-created private LYRICVI    
    EW Lyricview;    
    Private MediaPlayer MediaPlayer;    
    Private button button;    
    Private SeekBar SeekBar;    
    Private String Mp3path;    
        private int interval=45;//lyrics each line at the interval @Override public void onCreate (Bundle savedinstancestate) {    
        Super.oncreate (savedinstancestate);    
        This.requestwindowfeature (Window.feature_no_title); GetWindow (). SetFlags (WindowmanageR.layoutparams.flag_fullscreen);    
        
        Setcontentview (R.layout.main);    
        
        Mp3path = Environment.getexternalstoragedirectory (). GetAbsolutePath () + "/lyricsync/1.mp3";    
        Lyricview = (Lyricview) Findviewbyid (R.ID.MYLRC);    
        MediaPlayer = new MediaPlayer ();    
        
        This.requestwindowfeature (Window.feature_no_title);    
        Resetmusic (Mp3path);    
        SERCHLRC ();    
        
        Lyricview.settextsize ();    
        Button = (button) Findviewbyid (R.id.button);    
        
        Button.settext ("Play");    
        SeekBar = (SeekBar) Findviewbyid (r.id.seekbarmusic); Seekbar.setonseekbarchangelistener (New Onseekbarchangelistener () {@Override public V    
        
            OID Onstoptrackingtouch (SeekBar SeekBar) {//TODO auto-generated method stub @Override public void Onstarttrackingtouch (SeekBar seEkbar) {//TODO auto-generated method stub} @Overr    
                IDE public void onprogresschanged (SeekBar SeekBar, int progress, Boolean fromuser) { TODO auto-generated Method stub if (fromuser) {Mediapla    
                    Yer.seekto (progress); Lyricview.setoffsety (220-lyricview.selectindex (Progress) * (Lyricview.getsizeword () + I    
        
                NTERVAL-1));    
        
        }    
            }    
        }); Button.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View    
                    V) {//TODO auto-generated Method stub if (mediaplayer.isplaying ()) {    
                    Button.settext ("Play");    
                Mediaplayer.pause ();    
          } else {          Button.settext ("suspend");    
                    Mediaplayer.start (); Lyricview.setoffsety (220-lyricview.selectindex (Mediaplayer.getcurrentposition ()) * (Lyri    
        
                Cview.getsizeword () + INTERVAL-1));    
        
        }    
            }    
        }); Mediaplayer.setoncompletionlistener (New Mediaplayer.oncompletionlistener () {@Override public    
                void Oncompletion (MediaPlayer MP) {Resetmusic (Mp3path);    
                Lyricview.settextsize ();    
                Lyricview.setoffsety (200);    
            Mediaplayer.start ();    
        }    
        });    
        Seekbar.setmax (Mediaplayer.getduration ());    
    New Thread (New Runable ()). Start ();    
        public void Serchlrc () {String LRC = Mp3path;    
        LRC = lrc.substring (0, Lrc.length ()-4). Trim () + ". LRC". Trim (); Lyricview.reAD (LRC);    
        Lyricview.settextsize ();    
    Lyricview.setoffsety (350);    
        public void Resetmusic (String path) {mediaplayer.reset ();    
            try {mediaplayer.setdatasource (Mp3path);    
        Mediaplayer.prepare (); catch (IllegalArgumentException e) {//TODO auto-generated catch block E.printstacktrac    
        E (); catch (IllegalStateException e) {//TODO auto-generated catch block E.printstacktrace ()    
        ;    
        catch (IOException e) {//TODO auto-generated catch block E.printstacktrace (); } class Runable implements Runnable {@Override public void R Un () {//TODO auto-generated Method Stub (true) {try {Thread.Sleep(100); if (mediaplayer.isplaying ()) {lyricview.setoffsety (Lyricview.getoffsety ())-LYRICVIEW.SPEEDLR    
                        C ());    
                        Lyricview.selectindex (Mediaplayer.getcurrentposition ());    
                        Seekbar.setprogress (Mediaplayer.getcurrentposition ());    
                    Mhandler.post (Mupdateresults);    
                    The catch (Interruptedexception e) {//TODO auto-generated catch block    
                E.printstacktrace ();    
    }}} Handler Mhandler = new Handler ();     
        Runnable mupdateresults = new Runnable () {public void run () {lyricview.invalidate ();//Update view    
}    
    }; }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.