package smit.com.NetMusicInfo;
import java.io.BufferedReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.util.Log;
public class BaiDuSongSearch
{
private String TAG="Lrc Search";
private URL url;
public static final String DEFAULT_LOCAL = "GB2312";
StringBuffer sbUrlContent = new StringBuffer(); //存放URl数据
ArrayList SongListContent =new ArrayList(); //存放查找的歌的信息的。
//构造函数
public BaiDuSongSearch()
{
}
/*
* 初期化,根据参数取得lrc的地址
*/
public ArrayList Search(String musicName, String singerName)
{
// 将空格替换成+号
// 将空格替换成+号
musicName = musicName.replace(' ', '+');
singerName = singerName.replace(' ', '+');
//SoSo搜索
try {
musicName = URLEncoder.encode(musicName,"GBK");
} catch (UnsupportedEncodingException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
singerName = URLEncoder.encode(singerName,"GBK");
} catch (UnsupportedEncodingException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
String strUrl = "http://box.zhangmen.baidu.com/x?op=12&count=1&title="
+ musicName + "$$" + singerName + "$$$$";
Log.d(TAG, "SearchLRC Url="+strUrl);
//生成URL地址
try {
url = new URL(strUrl);
} catch (Exception e1) {
e1.printStackTrace();
}
BufferedReader br = null;
String s;
//打开URL地址
try {
InputStreamReader in = new InputStreamReader(url.openStream());
Log.d(TAG,"the encode is "+in.getEncoding());
br = new BufferedReader(in);
} catch (IOException e1) {
Log.d(TAG, "br is null");
}
//读取URL数据
try {
while ((s = br.readLine()) != null) {
sbUrlContent.append(s + "/r/n");
}
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return GetSongInfo();
}
private ArrayList GetSongInfo()
{
int begin = 0, end = 0, number = 0;
SongListContent.clear();
//<url><encode>http://au.9you.com//download/song/sample/0713/k6eXmZermKGQopSirJugmqAy.mp3</encode><decode>budebuai_panweibo.mp3</decode><type>1</type><lrcid>8654</lrcid></url>
//利用正则表达式查找歌信息。
CSongInfo SongInfo=null;
int iSongCount=0,iAlbumNumber=0;
String strSongUrl="";
String strSongInfo="",strid="";
String AlbumId="";
Matcher mSongList =null,mSongInfo=null;
String SongRex="<url>//s*<encode>([^<]*)</encode><decode>([^<]*)</decode><type>[^<]*</type><lrcid>([^<]*)</lrcid></url>";
Pattern SongPattern =Pattern.compile(SongRex);
mSongList = SongPattern.matcher(sbUrlContent);
try {
while(mSongList.find()&& SongListContent.size()<=5)
{
iSongCount = mSongList.groupCount();
if(iSongCount>=3)
{
SongInfo = new CSongInfo();
SongInfo.SongName="";
SongInfo.Singer = "";
SongInfo.AlbumName ="";
strSongUrl=mSongList.group(1) ;
begin =strSongUrl.lastIndexOf("/");
SongInfo.SongUrl = strSongUrl.substring(0,begin);
SongInfo.SongUrl = SongInfo.SongUrl + "/"+mSongList.group(2);
strid = mSongList.group(3);
number = Integer.parseInt(strid);
SongInfo.LrcUrl = "http://box.zhangmen.baidu.com/bdlrc/" + number / 100
+ "/" + number + ".lrc";
SongInfo.AlbumCoverUrl="";
SongListContent.add(SongInfo);
}
}
}
catch(IllegalStateException e2)
{
Log.d(TAG, "GetSongInfo get group Exception= ",e2);
}
return SongListContent;
}
/*
* 根据lrc的地址,读取lrc文件流
* 生成歌词的ArryList
* 每句歌词是一个String
*/
public ArrayList FetchLyric(String LrcUrl)
{
//获取LRc Url地址
if(LrcUrl==null)
{
return null;
}
String geciURL = LrcUrl;
Log.d(TAG, "geciURL = " + geciURL);
ArrayList LrcContent =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)
{
//
LrcContent.add(s);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return LrcContent;
}
}
package smit.com.NetMusicInfo;
public class CSongInfo
{
String SongName;
String Singer;
String AlbumName;
String SongUrl;
String LrcUrl;
String AlbumCoverUrl;
}
package smit.com.NetMusicInfo;
import java.util.ArrayList;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
public class NetMusicInfo extends Activity {
/** Called when the activity is first created. */
ImageView AlbumeImageView =null;
TextView SongLrcView =null;
private String TAG="Lrc Search";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button)findViewById(R.id.buttontest);
button.setOnClickListener(listen_btn_up);
AlbumeImageView = (ImageView)findViewById(R.id.AlbumImageView);
SongLrcView = (TextView)findViewById(R.id.LrcText);
button.setOnClickListener(listen_btn_up);
}
private OnClickListener listen_btn_up = new OnClickListener(){
public void onClick(View v)
{
SoSoSongSearch Songsearch = new SoSoSongSearch();
ArrayList SongList=Songsearch.Search("花蝴蝶","蔡依林");
Log.d(TAG, "SongList size = " + SongList.size());
CSongInfo SongInfo =(CSongInfo) SongList.get(0);
Bitmap bm=Songsearch.FetchAlbumCover(SongInfo.AlbumCoverUrl);
AlbumeImageView.setImageBitmap(bm);
ArrayList SongLrc=Songsearch.FetchLyric(SongInfo.LrcUrl);
SongLrcView.setText(SongLrc.toString());
/*
BaiDuSongSearch Songsearch = new BaiDuSongSearch();
ArrayList SongList=Songsearch.Search("不得不爱","潘玮柏");
Log.d(TAG, "SongList size = " + SongList.size());
CSongInfo SongInfo =(CSongInfo) SongList.get(0);
ArrayList SongLrc=Songsearch.FetchLyric(SongInfo.LrcUrl);
SongLrcView.setText(SongLrc.toString());
//Bitmap bm=Songsearch.FetchAlbumCover(SongInfo.AlbumCoverUrl);
//AlbumeImageView.setImageBitmap(bm);
*/
}
};
}
package smit.com.NetMusicInfo;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.nio.CharBuffer;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
public class SoSoSongSearch
{
private String TAG="Lrc Search";
private URL url;
public static final String DEFAULT_LOCAL = "GB2312";
StringBuffer sbUrlContent = new StringBuffer(512*1024); //存放URl数据, 初始容量512k
ArrayList SongListContent =new ArrayList(); //存放查找的歌的信息的。
//构造函数
public SoSoSongSearch()
{
}
/*
* 搜索歌词信息,返回歌词信息的封面。
*/
public ArrayList Search(String musicName, String singerName)
{
// 将空格替换成+号
musicName = musicName.replace(' ', '+');
singerName = singerName.replace(' ', '+');
//SoSo搜索
try {
musicName = URLEncoder.encode(musicName,"GBK");
} catch (UnsupportedEncodingException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
singerName = URLEncoder.encode(singerName,"GBK");
} catch (UnsupportedEncodingException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
String strUrl = "http://cgi.music.soso.com/fcgi-bin/m.q?w="
+ musicName + singerName + "&p=1&source=1&t=1";
Log.d(TAG, "SearchLRC Url="+strUrl);
//生成URL地址
try {
url = new URL(strUrl);
} catch (Exception e1) {
e1.printStackTrace();
}
BufferedReader br = null;
String s;
//打开URL地址
try {
//构造流
InputStreamReader in = new InputStreamReader(url.openStream(),"gb2312");
//Log.d(TAG,"the encode is "+in.getEncoding());
// 构造读取器
br = new BufferedReader(in);
} catch (IOException e1) {
Log.d(TAG, "Search br is null");
}
//读取URL数据
try {
while ((s = br.readLine()) != null) {
sbUrlContent.append(s+"/r/n");
}
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return GetSongInfo();
}
/*
*根据网页内容匹配歌词和专辑封面
*/
private ArrayList GetSongInfo()
{
//清除列表信息。
SongListContent.clear();
//利用正则表达式查找歌信息。
CSongInfo SongInfo=null;
int iSongCount=0,iAlbumNumber=0;
String strSongInfo="";
String StrTmp="",StrTmp1="";
String AlbumId="";
Matcher mSongList =null,mSongInfo=null;
String SongRex="<td//s*class=/"data/">([^<]*)</td>";
String SongInfoRex="([^@]*)@@([^@]*)@@([^@]*)@@([^@]*)@@([^@]*)@@([^@]*)@@([^@]*)@@([^@]*)@@([^@]*)@@([^@]*)@@([^@]*)@@([^@]*)";
Pattern SongPattern =Pattern.compile(SongRex);
Pattern SongInfoattern =Pattern.compile(SongInfoRex);
mSongList = SongPattern.matcher(sbUrlContent);
//745090248@@123木头人@@K情歌4 冠军主题歌@@黑Girl@@3741614@@mp3@@80@@233@@FIhttp://file.kehou.com/upload/musics/50/87/16/1002168750.mp3;浙江电信;http://aud01.p2v.tudou.com/191/171/090208120013_5290.mp3;电信;|SIhttp://pic1.aoecn.cn/grademusic/000/000/116/924359d1-0257-4b69-8c20-aa89d7ed51ac.mp3;浙江电信;http://sing.wingw.com/songs/juesai/123mutourenyyw.mp3;四川电信;http://common.jbch.org/upload/board/church/87/2010/87_20100806205729396_yh20020510.mp3;;||QQhttp://stream5.qqmusic.qq.com/12419440.wma;;@@3630769240@@2846234126@@0
//2147483647@@123@@如果未来@@Fusion@@3666381@@mp3@@100@@228@@FIhttp://upload6.music.qzone.soso.com/12416429.wma;;|||@@2503659731@@642711698@@0
try {
while(mSongList.find()&& SongListContent.size()<=5)
{
iSongCount = mSongList.groupCount();
if(iSongCount>=1)
{
strSongInfo = mSongList.group(1);
mSongInfo = SongInfoattern.matcher(strSongInfo);
if(mSongInfo.find())
{
int iGroupCount1=mSongInfo.groupCount();
if(iGroupCount1>=11)
{
SongInfo = new CSongInfo();
SongInfo.SongName=mSongInfo.group(2);
SongInfo.Singer = mSongInfo.group(4);
SongInfo.AlbumName = mSongInfo.group(3);
SongInfo.SongUrl = mSongInfo.group(9);
//中文需要编码一下的。
StrTmp = SongInfo.SongName;
try {
StrTmp = URLEncoder.encode(StrTmp,"GBK");
} catch (UnsupportedEncodingException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
StrTmp1 = SongInfo.Singer;
try {
StrTmp1 = URLEncoder.encode(StrTmp1,"GBK");
} catch (UnsupportedEncodingException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
SongInfo.LrcUrl="http://cgi.music.soso.com/fcgi-bin/fcg_download_lrc.q?song="+StrTmp+"&singer="+StrTmp1+"&down=1";
AlbumId = mSongInfo.group(11);
AlbumId = mSongInfo.group(11).substring(AlbumId.length()-2, AlbumId.length());
iAlbumNumber = Integer.parseInt(AlbumId);
SongInfo.AlbumCoverUrl="http://cache.music.soso.com/sosocache/music/pic/albumpic/150/"+iAlbumNumber+"/"+mSongInfo.group(11)+".jpg";
SongListContent.add(SongInfo);
}
}
}
}
}
catch(IllegalStateException e2)
{
Log.d(TAG, "GetSongInfo get group Exception= ",e2);
}
return SongListContent;
}
/*
*处理歌词
*/
public ArrayList FetchLyric(String Lrcurl)
{
//获取LRc Url地址
if(Lrcurl==null)
{
return null;
}
String geciURL = Lrcurl;
Log.d(TAG, "FetchLyric url = " + geciURL);
ArrayList LrcContent =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)
{
//
LrcContent.add(s);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return LrcContent;
}
public Bitmap FetchAlbumCover(String AlbumCoverurl)
{
Bitmap bm=null;
//获取LRc Url地址
if(AlbumCoverurl==null)
{
return null;
}
String geciURL = AlbumCoverurl;
Log.d(TAG, "FetchAlbumCover url = " + geciURL);
String s = new String();
try {
url = new URL(geciURL);
} catch (MalformedURLException e2)
{
e2.printStackTrace();
}
try
{
URLConnection con = url.openConnection();
con.connect();
InputStream is = con.getInputStream();
/* 建立缓冲区是一个良好的编程习惯. */
BufferedInputStream bis = new BufferedInputStream(is);
/* 解析网络上的图片 */
bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
}
catch (IOException e)
{
Log.e(TAG, "FetchAlbumCover Remtoe Image Exception=", e);
}
return bm;
}
}