一個Android下的自動下載歌詞的代碼

1. 原理是用Baidu音樂搜索的連接,拼參數進去讀取歌詞。

同理也可以一樣讀取到音樂文件。

代碼不是很難,但是網上基本上看不到這種代碼,有的也是不能運行的。

所以我就做個好事吧。

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);




    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章