同理也可以一样读取到音乐文件。
代码不是很难,但是网上基本上看不到这种代码,有的也是不能运行的。
所以我就做个好事吧。
2. 搜索歌词文件的代码:
package com.hyronjs.jiangbiao; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import android.util.Log; public class SearchLRC { private URL url; public static final String DEFAULT_LOCAL = "GB2312"; StringBuffer sb = new StringBuffer(); /* * 初期化,根据参数取得lrc的地址 */ public SearchLRC(String musicName, String singerName) { // 将空格替换成+号 musicName = musicName.replace(' ', '+'); singerName = singerName.replace(' ', '+'); String strUrl = "http://box.zhangmen.baidu.com/x?op=12&title=" + musicName + "$$" + singerName + "$$$$"; Log.d("test", strUrl); try { url = new URL(strUrl); } catch (Exception e1) { e1.printStackTrace(); } BufferedReader br = null; String s; try { InputStreamReader in = new InputStreamReader(url.openStream()); Log.d("the encode is ", in.getEncoding()); br = new BufferedReader(in); } catch (IOException e1) { Log.d("tag", "br is null"); } try { while ((s = br.readLine()) != null) { sb.append(s + "/r/n"); br.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /* * 根据lrc的地址,读取lrc文件流 * 生成歌词的ArryList * 每句歌词是一个String */ public ArrayList fetchLyric() { int begin = 0, end = 0, number = 0;// number=0表示暂无歌词 String strid = ""; begin = sb.indexOf("<lrcid>"); Log.d("test", "sb = " + sb); if (begin != -1) { end = sb.indexOf("</lrcid>", begin); strid = sb.substring(begin + 7, end); number = Integer.parseInt(strid); } String geciURL = "http://box.zhangmen.baidu.com/bdlrc/" + number / 100 + "/" + number + ".lrc"; Log.d("test", "geciURL = " + geciURL); ArrayList gcContent =new ArrayList(); String s = new String(); try { url = new URL(geciURL); } catch (MalformedURLException e2) { e2.printStackTrace(); } BufferedReader br = null; try { br = new BufferedReader(new InputStreamReader(url.openStream(), "GB2312")); } catch (IOException e1) { e1.printStackTrace(); } if (br == null) { System.out.print("stream is null"); } else { try { while ((s = br.readLine()) != null) { // Sentence sentence = new Sentence(s); gcContent.add(s); } br.close(); } catch (IOException e) { e.printStackTrace(); } } return gcContent; } }
3. 使用搜索的Active的例子
package com.hyronjs.jiangbiao; import java.util.ArrayList; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class GetSongWord extends Activity { private TextView mTextView1; /** Called when the activity is first created. */ private DrawLRC mGameView; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Baidu search = new Baidu("love the way you","michael jackson"); SearchLRC search = new SearchLRC("love the way you",""); ArrayList result = search.fetchLyric(); setContentView(R.layout.main); } }
转自:
http://blog.csdn.net/nanjingjiangbiao/article/details/6096376