Android Music player Development Sweetmusicplayer Smart load in-line lyrics

Source: Internet
Author: User

use MediaPlayer to play music in a written, http://blog.csdn.net/huweigoodboy/article/details/39862773. What if there is no local lyrics? Now let's load the lyrics online. OK, still use that picture.



In the implementation of this function, LZ tried Baidu API, lyrics fan API, later selected lyrics fan API. Although the resources are still not complete. And there are very many errors.

The special headache is that sometimes lyrics do not branch. It is simply uncomfortable to parse.


Lyrics Fan API lyrics query address: http://geci.me/api/lyric/


For example I want to query: http://geci.me/api/lyric/quiet/Jay Chou


Will get a JSON string:

{"Count": 2, "code": 0, "result": [{"Aid": 2223011, "artist_id": 30796, "song": "\u5b89\u9759", "LRC": "http://s.geci.me/l RC/257/25700/2570058.LRC "," Sid ": 2570058}, {" Aid ": 2336033," artist_id ": 30796," song ":" \u5b89\u9759 "," LRC ":"/HTTP/ S.GECI.ME/LRC/272/27282/2728244.LRC "," Sid ": 2728244}]}

Very easy to find inside the lyrics file. Then buffer to local (sweetmusicplayer/lryics), and then press the local load mode.


We'll go through the following steps when we load the lyrics file.

1) The address of the lyrics is queried by the address.

(Here the landlord with URLConnection)

2) Buffer lyrics file with lyrics address.

(Here the landlord with URLConnection)

3) Load the buffered lyrics file.


Said above seems to be more easy, the landlord himself wrote a demo, is a javaproject, found no problem. Load the lyrics file normally.

Wait until Android, the first step on your knees. Discovering URLConnection's getInputStream () throws an IO exception, which is simply deadly. Toss for a while to find is due to bring http://geci.me/api/lyric/quiet/Jay Chinese path.

Since the default is GBK, the network transmission is utf-8, so the Chinese transcoding. Urlencoder.encode (str, "utf-8");


To the 2nd step, the problem appeared again, the lyrics garbled.

The solution, using the character stream operation is more appropriate, but also pay attention to the same code.

Package Com.huwei.sweetmusicplayer.util;import Java.io.bufferedreader;import Java.io.bufferedwriter;import Java.io.file;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.io.InputStreamReader;import Java.io.outputstreamwriter;import Java.io.printwriter;import Java.io.unsupportedencodingexception;import Java.net.httpurlconnection;import Java.net.malformedurlexception;import Java.net.url;import Java.net.urlconnection;import Java.net.urlencoder;import Java.util.random;import Org.json.JSONArray;import Org.json.jsonexception;import Org.json.jsonobject;import Android.os.environment;import Android.util.Log;public  Class Onlinelrcutil {private static String TAG = "Onlinelrcutil";p rivate static onlinelrcutil instance;public static final String Lrcrootpath = Environment.getexternalstoragedirectory (). toString () + "/sweetmusicplayer/lyrics/";p ublic Static final String querylrcurlroot = "http://geci.me/api/lyric/";p ublic static Onlinelrcutil getinstance () {if (null = = I Nstance) {iNstance = new Onlinelrcutil ();} return instance;} public string Getquerylrcurl (string title, string artist) {return querylrcurlroot + Encode (title) + "/" + Encode (artist);}  public string Getlrcurl (string title, string artist) {String querylrcurlstr = Getquerylrcurl (title, artist); try {URL url = New URL (QUERYLRCURLSTR); URLConnection urlconnection = Url.openconnection (); Urlconnection.connect (); BufferedReader in = new BufferedReader (New InputStreamReader (Urlconnection.getinputstream ())); StringBuffer sb = new StringBuffer (); String temp;while (temp = In.readline ())! = null) {sb.append (temp);} Jsonobject jobject = new Jsonobject (sb.tostring ()), int count = Jobject.getint ("count"); int index = Count = = 0? 0:new Random (). Nextint ()% count; Jsonarray Jarray = Jobject.getjsonarray ("result"); Jsonobject obj = jarray.getjsonobject (index); return obj.getstring ("LRC"); catch (Malformedurlexception e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//Todo Au to-generated CAtch blocke.printstacktrace ();} catch (Jsonexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} return null;} Singer, a space in the name of the song transcoding public string Encode (String str) {try {return Urlencoder.encode (Str.trim (), "Utf-8");} catch ( Unsupportedencodingexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} return str;} Lyrics file network address, lyrics file local buffer address public boolean wrtiecontentfromurl (String URLPath, String lrcpath) {log.i (TAG, "Lrcurl" + URLPath) ; try {URL url = new URL (urlpath); URLConnection urlconnection = Url.openconnection (); Urlconnection.connect (); HttpURLConnection httpconn = (httpurlconnection) urlconnection;if (httpconn.getresponsecode () = = HTTPURLCONNECTION.HTTP_OK) {File File = new file (Lrcrootpath), if (!file.exists ()) {file.mkdirs ();} BufferedReader bf = new BufferedReader (New InputStreamReader (Urlconnection.getinputstream (), "Utf-8")); PrintWriter out = new PrintWriter (new BufferedWriter (New FileOutputStream (Lrcpath), "Utf-8")); Char c[] = new char[256];int temp = -1;while ((temp = Bf.read ())! =-1) {bf.read (c); Out.write (c);} Bf.close (); Out.close (); return true;} System.out.println ("GetFile:" +str);} catch (Malformedurlexception e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//Todo Au To-generated catch Blocke.printstacktrace ();} return false;} public string Getlrcpath (string title, string artist) {return Lrcrootpath + title + "-" + artist + ". LRC";}}


The next article shakes a song: http://blog.csdn.net/huweigoodboy/article/details/39880779

Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Android Music player Development Sweetmusicplayer Smart load in-line lyrics

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.