一个Android下的自动下载歌词的代码

1. 原理是用Baidu音乐搜索的连接,拼参数进去读取歌词。

同理也可以一样读取到音乐文件。

代码不是很难,但是网上基本上看不到这种代码,有的也是不能运行的。

所以我就做个好事吧。

2. 搜索歌词文件的代码:

Java代码 
  1. package com.hyronjs.jiangbiao;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.IOException;  
  5. import java.io.InputStreamReader;  
  6. import java.net.MalformedURLException;  
  7. import java.net.URL;  
  8. import java.util.ArrayList;  
  9.   
  10.   
  11.   
  12. import android.util.Log;  
  13.   
  14. public class SearchLRC {  
  15.     private URL url;  
  16.   
  17.     public static final String DEFAULT_LOCAL = "GB2312";  
  18.     StringBuffer sb = new StringBuffer();  
  19.   
  20.     /* 
  21.      * 初期化,根据参数取得lrc的地址 
  22.      */  
  23.     public SearchLRC(String musicName, String singerName) {  
  24.         // 将空格替换成+号  
  25.         musicName = musicName.replace(' ''+');  
  26.         singerName = singerName.replace(' ''+');  
  27.         String strUrl = "http://box.zhangmen.baidu.com/x?op=12&title="  
  28.                 + musicName + "$$" + singerName + "$$$$";  
  29.         Log.d("test", strUrl);  
  30.         try {  
  31.             url = new URL(strUrl);  
  32.         } catch (Exception e1) {  
  33.             e1.printStackTrace();  
  34.         }  
  35.         BufferedReader br = null;  
  36.         String s;  
  37.         try {  
  38.             InputStreamReader in = new InputStreamReader(url.openStream());  
  39.             Log.d("the encode is ", in.getEncoding());  
  40.             br = new BufferedReader(in);  
  41.         } catch (IOException e1) {  
  42.             Log.d("tag""br is null");  
  43.         }  
  44.         try {  
  45.             while ((s = br.readLine()) != null) {  
  46.                 sb.append(s + "\r\n");  
  47.                 br.close();  
  48.             }  
  49.         } catch (IOException e) {  
  50.             // TODO Auto-generated catch block  
  51.             e.printStackTrace();  
  52.         }  
  53.   
  54.     }  
  55.   
  56.     /* 
  57.      * 根据lrc的地址,读取lrc文件流 
  58.      * 生成歌词的ArryList 
  59.      * 每句歌词是一个String 
  60.      */  
  61.     public ArrayList fetchLyric() {  
  62.         int begin = 0, end = 0, number = 0;// number=0表示暂无歌词  
  63.         String strid = "";  
  64.         begin = sb.indexOf("<lrcid>");  
  65.         Log.d("test""sb = " + sb);  
  66.         if (begin != -1) {  
  67.             end = sb.indexOf("</lrcid>", begin);  
  68.             strid = sb.substring(begin + 7, end);  
  69.             number = Integer.parseInt(strid);  
  70.         }  
  71.   
  72.         String geciURL = "http://box.zhangmen.baidu.com/bdlrc/" + number / 100  
  73.                 + "/" + number + ".lrc";  
  74.         Log.d("test""geciURL = " + geciURL);  
  75.         ArrayList gcContent =new ArrayList();  
  76.         String s = new String();  
  77.         try {  
  78.             url = new URL(geciURL);  
  79.         } catch (MalformedURLException e2) {  
  80.             e2.printStackTrace();  
  81.         }  
  82.   
  83.         BufferedReader br = null;  
  84.         try {  
  85.             br = new BufferedReader(new InputStreamReader(url.openStream(), "GB2312"));  
  86.         } catch (IOException e1) {  
  87.             e1.printStackTrace();  
  88.         }  
  89.         if (br == null) {  
  90.             System.out.print("stream is null");  
  91.         } else {  
  92.             try {  
  93.                 while ((s = br.readLine()) != null) {  
  94. //                  Sentence sentence = new Sentence(s);  
  95.                     gcContent.add(s);  
  96.                       
  97.                 }  
  98.                 br.close();  
  99.             } catch (IOException e) {  
  100.                 e.printStackTrace();  
  101.             }  
  102.   
  103.         }  
  104.         return gcContent;  
  105.     }  
  106.   
  107. }  

3. 使用搜索的Active的例子

Java代码 
  1. package com.hyronjs.jiangbiao;  
  2.   
  3. import java.util.ArrayList;  
  4.   
  5. import android.app.Activity;  
  6. import android.os.Bundle;  
  7. import android.widget.TextView;  
  8.   
  9. public class GetSongWord extends Activity {  
  10.     private TextView mTextView1;  
  11.      /** Called when the activity is first created. */    
  12.     private DrawLRC mGameView;     
  13.   
  14.       
  15.     /** Called when the activity is first created. */  
  16.     @Override  
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19. //      Baidu search = new Baidu("love the way you","michael jackson");  
  20.         SearchLRC search = new SearchLRC("love the way you","");  
  21.         ArrayList result = search.fetchLyric();  
  22.           
  23.         setContentView(R.layout.main);  
  24.   
  25.   
  26.     }  
  27. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值