Android Practice Project MP3 player for lyrics sync play (iv)

Source: Internet
Author: User
Tags drawtext

In fact, to the back we need how to display lyrics, for the lyrics of the synchronization display is better to achieve, mainly by judging the current playing time and the length of the lyrics of each node, to synchronize the data corresponding to the node, now is how to achieve this problem.

In fact, this time you need to customize the control to implement.
The first step is to customize the view's properties.
The second step is to implement the properties that we have customized in the view's construction method.
Primarily through initialization functions, init ()
The third step is to override the OnDraw function.

Lrcview.java

 PackageCom.flashmusic.View;ImportAndroid.content.Context;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Color;ImportAndroid.graphics.Paint;ImportAndroid.graphics.Typeface;ImportAndroid.util.AttributeSet;ImportAndroid.widget.TextView;ImportCom.flashmusic.tool.LrcInfo;ImportCom.flashmusic.tool.LrcList;ImportJava.util.Map;/** * Created by Zhouchenglin on 2016/4/20. * * Public  class lrcview extends TextView {    Private floatWidth//Lyrics View width    Private floatHeight//Lyrics View height    PrivatePaint Currentpaint;//Current Brush Object    PrivatePaint Notcurrentpaint;//non-current Brush object    Private floatTextHeight = $;//Text height    Private floatTextmaxsize = -;Private floatTextSize = +;//Text size    Private intindex =0;//list Set subscript    PrivateLrcinfo infos;//Lyrics Information     Public void setmlrclist(Lrcinfo infos) { This. infos = Infos; } Public Lrcview(Context context) {Super(context);    Init (); } Public Lrcview(context context, AttributeSet attrs,intDefstyle) {Super(Context, attrs, Defstyle);    Init (); } Public Lrcview(context context, AttributeSet attrs) {Super(context, attrs);    Init (); }Private void Init() {setfocusable (true);//Set to focus        //Show lyrics SectionCurrentpaint =NewPaint (); Currentpaint.setantialias (true);//Set anti-aliasing to make the text beautiful and fullCurrentpaint.settextalign (Paint.Align.CENTER);//Set text alignment        //non-highlighted partsNotcurrentpaint =NewPaint (); Notcurrentpaint.setantialias (true);    Notcurrentpaint.settextalign (Paint.Align.CENTER); }/** * Painting lyrics * *    @Override    protected void OnDraw(Canvas canvas) {Super. OnDraw (canvas);if(Canvas = =NULL) {return; } Currentpaint.setcolor (Color.argb ( About,251,248, in)); Notcurrentpaint.setcolor (Color.argb ( $,255,255,255));        Currentpaint.settextsize (textmaxsize);        Currentpaint.settypeface (Typeface.serif);        Notcurrentpaint.settextsize (textSize); Notcurrentpaint.settypeface (Typeface.default);Try{SetText (""); Canvas.drawtext (Infos.getlrclists (). Get (Index). GetContent (), Width/2, Height/2, Currentpaint);floatTempy = height/2;//Draw a sentence before this sentence                 for(inti = index-1; I >=0; i--) {//UpwardTempy = Tempy-textheight; Canvas.drawtext (Infos.getlrclists (). get (i). GetContent (), Width/2, Tempy, Notcurrentpaint); } tempy = height/2;//Draw a sentence after this sentence                 for(inti = index +1; I < infos.getlrclists (). Size (); i++) {//DownTempy = Tempy + textHeight; Canvas.drawtext (Infos.getlrclists (). get (i). GetContent (), Width/2, Tempy, Notcurrentpaint); }        }Catch(Exception e) {SetText ("... Wood has lyrics files, hurry to download ... "); }    }/** * method to call when the view size changes */    protected void onsizechanged(intWintHintOLDW,intOLDH) {Super. onsizechanged (W, H, OLDW, OLDH); This. width = w; This. height = h; } Public void Setindex(intIndex) { This. index = index; }}

This class is someone else's realization, I studied carefully, I think it is good to write, through the judgment of the subscript to achieve the text display, this is quite good, worth learning.

The next step is to get the initialization function in Musicservice.java.

  Public void INITLRC() {//Create lyrics ObjectLrcparse Lrcparser =NewLrcparse (path);//Read the lyrics and pass the data to the lyrics information classLrcinfo = LRCPARSER.READLRC ();//Get the nodes in the lyricsLrclists = Lrcinfo.getlrclists ();//Set static in Musicactivity to share dataMusicActivity.lrcView.setmLrcList (Lrcinfo);//Toggle Picture Display LyricsMusicActivity.lrcView.setAnimation (Animationutils.loadanimation (Musicservice. This, r.anim.alpha_z));    Mhandler.post (mrunnable); } Runnable mrunnable =NewRunnable () {@Override         Public void Run() {MusicActivity.lrcView.setIndex (Lrcindex ());            MusicActivity.lrcView.invalidate (); Mhandler.postdelayed (Mrunnable, -); }    };

Handle mainly through the runnable to achieve the musicactivity inside the lyrics synchronization, but synchronization still need to determine the custom control inside the Index property.

The Lrcindex function is judged by comparing time to obtain the specific index.

PublicintLrcindex () {if(Mediaplayer.isplaying ()) {currenttime= Mediaplayer.getcurrentposition ();        Duration = Mediaplayer.getduration (); }if(currenttime< duration) { for(inti =0; I < lrclists.size(); i++) {if(I < lrclists.size() -1) {if(currenttime< Lrclists.get (i). GetCurrentTime () && i = =0) {index = i; }if((currenttime> Lrclists.get (i). GetCurrentTime ()) &&currenttime< Lrclists.get (i+1). GetCurrentTime ()) {index = i; }                }if(i = = lrclists.size() -1) &&currenttime> Lrclists.get (i). GetCurrentTime ()) {index = i; }            }        }returnIndex }

The next step is to call the initialization function, because we show the lyrics synchronization every time we play, so I put him in the initialized function, the problem can be done.
Look at the play function, just add a line of code to complete.

 Private void Play(intCurrentTime) {Try{Mediaplayer.reset ();//Restore the parameters to their original stateMediaplayer.setdatasource (path); Mediaplayer.prepare ();//BufferingMediaplayer.setonpreparedlistener (NewPreparedlistener (currenttime));//Register a listenerINITLRC ();//Update playback statusIntent Intent =NewIntent (Play_statue);//Send the completed signal, update the playback statusIntent.putextra ("Playstatue",true);            Sendbroadcast (Intent); Mhandler.sendemptymessage (1); }Catch(Exception e)        {E.printstacktrace (); }    }

Look at the effect of the implementation:


Now a local music player function basically realized, here to use a lot of knowledge, the understanding of Android this system has deepened the understanding, thanks to the predecessors of the blog, let me benefit, thanks to the sharing of technology, now I also for more people, to share their blog. Let's keep improving in the ocean of technology.

Android Practice Project MP3 player for lyrics sync play (iv)

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.