Android搜索并下载歌词,thanks to www.lyricist.cn!

在android上,下面这段代码可以从乐辞服务器上抓取歌词并保存到本地,请不要用于商业目的!

http内容获取可以用多种方式,下面的代码使用HttpGet和URLConnection两种方式都可以奏效。

LrcCatcherLyricist.java
  1 import java.io.File;
2 import java.io.FileWriter;
3 import java.io.IOException;
4
5 import org.apache.http.HttpResponse;
6 import org.apache.http.HttpStatus;
7 import org.apache.http.client.HttpClient;
8 import org.apache.http.client.methods.HttpGet;
9 import org.apache.http.impl.client.DefaultHttpClient;
10 import org.apache.http.util.EntityUtils;
11
12 import android.util.Log;
13
14 public class LrcCatcherLyricist {
15 public String find(String ar, String ti, String store) {
16 String ret = null;
17 HttpClient httpClient = new DefaultHttpClient();
18
19 String sUrl = "http://www.winampcn.com/lrceng/get.aspx?song={ti}&artist={ar}&lsong={lti}&prec=1&Datetime=20060601";
20 sUrl = sUrl.replace("{ti}", ti).replace("{ar}", ar)
21 .replace("{lti}", ti);
22 Log.d("LrcCatcherLyricist Get", sUrl);
23
24 /**
25 * first way: using HttpGet.
26 */
27 HttpGet httpGet = new HttpGet(sUrl);
28 // set parameters.
29 // HttpParams httpParams = new BasicHttpParams();
30 // HttpConnectionParams.setConnectionTimeout(httpParams, 30000);
31 // HttpConnectionParams.setSoTimeout(httpParams, 30000);
32 // httpGet.setParams(httpParams);
33 try {
34 HttpResponse response = httpClient.execute(httpGet);
35 if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
36 ret = EntityUtils.toString(response.getEntity());
37 }
38 } catch (Exception e) {
39 return ret;
40 }
41 MusicTracker.d("LrcCatcherLyricist", ret);
42
43 /**
44 * second way: using URLConnection.
45 */
46 // a
47 // URL url = new URL(sUrl);
48 // URLConnection conn = url.openConnection();
49 //
50 // BufferedReader rd = new BufferedReader(new InputStreamReader(
51 // conn.getInputStream()));
52 // String line = "";
53 //
54 // while ((line = rd.readLine()) != null) {
55 // Log.d("LrcCatcherLyricist Get", line);
56 // }
57
58 /**
59 * get lyric down-load URL. just only get the first result.
60 * [CDATA[http://www.lyricist.cn/lrceng/lrc.aspx?id={id}&ti={ti}]]
61 */
62 // find "[CDATA[" and get lyric URL.
63 String sflag = "[CDATA[";
64 String eflag = "]]";
65 int s = ret.indexOf(sflag);
66 int e = ret.indexOf(eflag, s);
67 ret = ret.substring(s + sflag.length(), e);
68
69 /**
70 * write lyric to file system.
71 */
72 if (lrcDownload(ret, store)) {
73 return null;
74 } else {
75 return ret;
76 }
77 }
78
79 private boolean lrcDownload(String sUrl, String store) {
80 HttpClient httpClient = new DefaultHttpClient();
81 String ret = null;
82
83 Log.d("LrcCatcherLyricist", "lrcDownload:" + sUrl);
84
85 HttpGet httpGet = new HttpGet(sUrl);
86 try {
87 HttpResponse response = httpClient.execute(httpGet);
88 if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
89 ret = EntityUtils.toString(response.getEntity());
90 }
91 } catch (Exception e) {
92 return false;
93 }
94
95 File lrc = new File(store);
96 try {
97 lrc.createNewFile();
98 FileWriter fw = new FileWriter(lrc);
99 fw.write(ret);
100 fw.flush();
101 fw.close();
102 return true;
103 } catch (IOException e) {
104 e.printStackTrace();
105 }
106
107 return false;
108 }
109 }

闲来无事写的,不要用于商业目的。谢谢合作~~~

转载于:https://www.cnblogs.com/cxxx/archive/2012/02/03/2337008.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue.js 是一个流行的 JavaScript 框架,用于构建 Web 应用程序。要在 Vue.js 中播放 FLAC 音乐和歌词,您可以使用一些第三方库和插件。 首先,您需要使用一个音乐播放器库,如 Howler.js 或 Plyr,它们都支持 FLAC 音频格式,并且易于集成到 Vue.js 应用程序中。 然后,您可以使用一个歌词解析器库,如 Lyricist.js 或 js-lyrics,来解析歌词文件,并在页面上显示歌词。这些库可以将歌词文件解析为 JSON 格式,然后您可以使用 Vue.js 组件将其呈现在页面上。 以下是一个简单的示例组件,用于播放 FLAC 音乐和显示歌词: ```html <template> <div> <audio ref="player" :src="audioSrc"></audio> <div v-for="line in lyrics" :key="line.time">{{ line.text }}</div> </div> </template> <script> import Howler from 'howler'; import Lyricist from 'lyricist'; export default { data() { return { audioSrc: 'path/to/audio.flac', lyrics: [], player: null, currentLine: null, }; }, mounted() { this.player = new Howler.Howl({ src: [this.audioSrc], format: ['flac'], onplay: this.handlePlay, onend: this.handleEnd, }); }, methods: { handlePlay() { const lyricist = new Lyricist(); lyricist.fetch('path/to/lyrics.lrc').then((lyrics) => { this.lyrics = lyricist.parse(lyrics); this.currentLine = 0; }); }, handleEnd() { this.currentLine = null; }, handleTimeupdate() { const time = this.$refs.player.currentTime(); const lineIndex = this.lyrics.findIndex((line) => line.time > time); if (lineIndex !== this.currentLine) { this.currentLine = lineIndex - 1; } }, }, created() { this.$refs.player.addEventListener('timeupdate', this.handleTimeupdate); }, beforeDestroy() { this.$refs.player.removeEventListener('timeupdate', this.handleTimeupdate); }, }; </script> ``` 请注意,此示例仅用于演示目的,并且可能需要进行一些修改才能适应您的应用程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值