The previous article wrote about using MediaPlayer to play music, 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, and later chose the lyrics fan API, although the resources are not complete, and there are many errors. The special headache is that sometimes the lyrics actually do not branch, parsing is simply uncomfortable.
Lyrics Fan API lyrics query address: http://geci.me/api/lyric/
For example I want to inquire: 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}]}
It is easy to find the lyrics file inside, then buffer it to local (sweetmusicplayer/lryics), then press the local load mode.
Smoothed, we load the lyrics file to go through the following steps.
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 relatively easy, the landlord himself wrote a demo, is a Java project, found no problem, normal load lyrics files.
Wait until Android, the first step on your knees. Found URLConnection getInputStream () throws an IO exception, simply deadly, tossing a half-day only to find out is because with the http://geci.me/api/lyric/quiet/Jay Chinese path. Since the default is GBK, the network transmission for Utf-8, so to the Chinese transcoding, Urlencoder.encode (str, "utf-8");
To the 2nd step, the problem appeared again, the lyrics garbled. solution, use a character stream operation 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";}}
Android Music player Development Sweetmusicplayer smart load Online lyrics