android音樂播放器開發 SweetMusicPlayer 智能載入線上歌詞,android播放器
上一篇寫了使用MediaPlayer播放音樂, http://blog.csdn.net/huweigoodboy/article/details/39862773,如果沒有本地歌詞怎麼辦?現在來將一下載入線上歌詞。好了,還是用那張圖。
在實現這個功能的時候,lz嘗試過baidu api,歌詞迷api,後來選用了歌詞迷api,雖然還是資源不全,而且還有很多錯誤。特別頭疼的是有時候歌詞居然不分行,解析起來簡直難受。
歌詞迷api歌詞查詢地址:http://geci.me/api/lyric/
比如我要查詢: http://geci.me/api/lyric/安靜/周杰倫
會得到一下json串:
{"count": 2, "code": 0, "result": [{"aid": 2223011, "artist_id": 30796, "song": "\u5b89\u9759", "lrc": "http://s.geci.me/lrc/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}]}
很容易發現裡面的歌詞檔案,然後緩衝到本地(SweetMusicPlayer/Lryics)下,再按本地載入的方式就行了。
捋一捋,我們載入歌詞檔案要經過以下步驟。
1)通過地址查詢出歌詞的地址。(這裡樓主用URLConnection)
2)通過歌詞地址緩衝歌詞檔案。(這裡樓主用URLConnection)
3)載入緩衝好的歌詞檔案。
上面說的看起來還是比較容易,樓主自己寫了個demo,是一個java工程,發現沒啥問題,正常載入歌詞檔案。
等到android上,第一步就跪了。發現URLConnection的getInputStream()拋出一個io異常,簡直要命,折騰了半天才發現是因為帶了http://geci.me/api/lyric/安靜/周杰倫中文路徑。由於預設是gbk,網路傳輸為utf-8,所以要把中文轉碼,URLEncoder.encode(str,"utf-8");即可。
到了第2步,問題又出現了,歌詞亂碼。解決辦法,用字元流操作比較合適,還要注意同一編碼。
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";private static OnlineLrcUtil instance;public static final String lrcRootPath = Environment.getExternalStorageDirectory().toString()+ "/SweetMusicPlayer/Lyrics/";public static final String queryLrcURLRoot = "http://geci.me/api/lyric/";public static OnlineLrcUtil getInstance() {if (null == instance) {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 Auto-generated catch blocke.printStackTrace();} catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();}return null;}// 歌手,歌曲名中的空格進行轉碼public String Encode(String str) {try {return URLEncoder.encode(str.trim(), "utf-8");} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke.printStackTrace();}return str;}// 歌詞檔案網路地址,歌詞檔案本地緩衝地址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 OutputStreamWriter(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 Auto-generated catch blocke.printStackTrace();}return false;}public String getLrcPath(String title, String artist) {return lrcRootPath + title + " - " + artist + ".lrc";}}
下一篇搖一搖切歌:http://blog.csdn.net/huweigoodboy/article/details/39880779
Android項目音樂播放器中怎實現歌詞同步
據我所知,無法實現,
安卓原生音樂播放器 可以顯示歌詞的
android原生音樂沒有這個功能。
百度雲ROM、MIUI這些基於原生的第三方ROM內建的音樂有歌詞功能。一般不支援在案頭顯示。
一些第三方聽歌應用如酷狗,酷我有這種功能。通常支援在案頭顯示