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


    }
}

转自:
http://blog.csdn.net/nanjingjiangbiao/article/details/6096376
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值