码迷,mamicode.com
首页 > 其他好文 > 详细

音乐播放器

时间:2015-06-11 00:12:51      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

 

 

技术分享
  1 package com.tan.lyc;
  2 
  3 import java.io.BufferedInputStream;
  4 import java.io.BufferedReader;
  5 import java.io.ByteArrayOutputStream;
  6 import java.io.File;
  7 import java.io.FileInputStream;
  8 import java.io.FileNotFoundException;
  9 import java.io.FileOutputStream;
 10 import java.io.FileWriter;
 11 import java.io.IOException;
 12 import java.io.InputStream;
 13 import java.io.InputStreamReader;
 14 import java.io.Reader;
 15 import java.io.UnsupportedEncodingException;
 16 import java.net.HttpURLConnection;
 17 import java.net.MalformedURLException;
 18 import java.net.ProtocolException;
 19 import java.net.URL;
 20 import java.util.ArrayList;
 21 import java.util.List;
 22 import java.net.HttpURLConnection;
 23 import java.net.MalformedURLException;
 24 import java.net.URLEncoder;
 25 
 26 import org.json.JSONArray;
 27 import org.json.JSONException;
 28 import org.json.JSONObject;
 29 
 30 import com.tan.myplay.MyApplication;
 31 import com.tan.service.MusicPlayService;
 32 
 33 import android.content.IntentSender.SendIntentException;
 34 import android.util.Log;
 35 
 36 public class LrcProcess {
 37     private String url = "http://geci.me/api/lyric/";
 38     private List<LrcContent> lrcList; // List集合存放歌词内容对象
 39     private LrcContent mLrcContent; // 声明一个歌词内容对象
 40     private String lrc;
 41     private ByteArrayOutputStream out;
 42     private String songname, singer;
 43 
 44     /**
 45      * 无参构造函数用来实例化对象
 46      */
 47     public LrcProcess() {
 48         mLrcContent = new LrcContent();
 49         lrcList = new ArrayList<LrcContent>();
 50     }
 51 
 52     /**
 53      * 读取歌词
 54      * 
 55      * @param path
 56      * @param songname
 57      * @param singer
 58      * @return
 59      */
 60     public String readLRC(String path, String singer, String songname) {
 61         // 定义一个StringBuilder对象,用来存放歌词内容
 62         StringBuilder stringBuilder = new StringBuilder();
 63         String s1 = path.substring(0, path.length() - 4);
 64         s1 += ".lrc";
 65         Log.d("test", s1 + "           path");
 66         Log.d("test","please tell me ok");
 67         File f = new File(s1);
 68         // System.out.println(path);
 69         try {
 70             FileInputStream fis = new FileInputStream(f);
 71             InputStreamReader isr = new InputStreamReader(fis, "utf-8");
 72             BufferedReader br = new BufferedReader(isr);
 73             String s = "";
 74             while ((s = br.readLine()) != null) {
 75                 s = s.replace("[", "");
 76                 s = s.replace("]", "@"); // 每一句话的分隔符
 77 
 78                 s = s.replaceAll("<[0-9]{3,5}>", ""); // 去掉每个字的时间标签,这里用到了正则表达式
 79 
 80                 String spiltLrcData[] = s.split("@");
 81 
 82                 if (spiltLrcData.length > 1) {
 83 
 84                     mLrcContent.setLrcStr(spiltLrcData[1]); // 将每句话创建一个类的实例,歌词和对应时间赋值
 85                     int lycTime = time2Str(spiltLrcData[0]);
 86                     mLrcContent.setLrcTime(lycTime);
 87                     lrcList.add(mLrcContent);
 88 
 89                     mLrcContent = new LrcContent();
 90                 }
 91             }
 92         } catch (FileNotFoundException e) {
 93             e.printStackTrace();
 94             // Log.d("test", "findlrc");
 95 //             findLrc(path, songname, singer);
 96             // Log.d("test", "why not");
 97             test(path,songname, singer);
 98             // readLRC(path, songname, singer);
 99             stringBuilder.append("一丁点儿歌词都没找到,下载后再来找我把.......");
100         } catch (Exception e) {
101             e.printStackTrace();
102             stringBuilder.append("没有读取到歌词.....");
103         }
104         return stringBuilder.toString();
105     }
106 
107     private void test(final String path,final String songname, final String singer) {
108         out = new ByteArrayOutputStream();
109         new Thread() {
110             public void run() {
111                 String url = null;
112                 try {
113                     url = setParams(songname, singer);
114                 } catch (UnsupportedEncodingException e1) {
115                     
116                     e1.printStackTrace();
117                 }
118                 try {
119                     URL url1 = new URL(url);
120                     HttpURLConnection conn = (HttpURLConnection) url1
121                             .openConnection();
122                     InputStream in = conn.getInputStream();
123                     byte[] data = new byte[1024];
124                     int len = 0;
125 
126                     while ((len = in.read(data)) != -1) {
127                         Log.d("test", data + "data");
128                         out.write(data, 0, len);
129                     }
130                     out.flush();
131                     
132                     String json=new String(out.toByteArray());
133                     Log.d("test", new String(out.toByteArray()));
134                     
135                     JSONObject jsonObject = null;
136                     try {
137                         jsonObject = new JSONObject(json);
138                     } catch (JSONException e1) {
139                         // TODO Auto-generated catch block
140                         e1.printStackTrace();
141                     }
142                     int count = 0;
143                     try {
144                         count = jsonObject.getInt("count");
145                     } catch (JSONException e) {
146                         // TODO Auto-generated catch block
147                         e.printStackTrace();
148                     }
149                     Log.d("count", count + "");
150                     JSONArray array = null;
151                     try {
152                         array = jsonObject.getJSONArray("result");
153                     } catch (JSONException e) {
154                         // TODO Auto-generated catch block
155                         e.printStackTrace();
156                     }
157                     try {
158                         lrc = array.getJSONObject(0).getString("lrc");
159                     } catch (JSONException e) {
160                         // TODO Auto-generated catch block
161                         e.printStackTrace();
162                     }
163                     
164                     URL url2 = new URL(lrc);
165                     InputStream in1 = url2.openStream();
166                     BufferedReader reader = new BufferedReader(
167                             new InputStreamReader(in1));
168                     String s1 = path.substring(0, path.length() - 4);
169                     s1 += ".lrc";
170                 
171                     Log.d("test",s1+"file path");
172                     File file = new File(s1);
173                     // FileOutputStream fout=new FileOutputStream(file);
174                     FileWriter fw = new FileWriter(file);
175                     String s = "";
176                     while ((s = reader.readLine()) != null) {
177                         fw.write(s+"\n");
178                         Log.d("test", s);
179                     }
180                     fw.close();
181 
182                 } catch (MalformedURLException e) {
183                     // TODO Auto-generated catch block
184                     e.printStackTrace();
185                 } catch (IOException e) {
186                     // TODO Auto-generated catch block
187                     e.printStackTrace();
188                 }
189             }
190         }.start();
191 
192     }
193 
194     private void findLrc(final String path, String songname, String singer) {
195 
196         String json = getJson(songname, singer);
197 
198         Log.d("test", json + "json");
199         String lrc = analysis(json);
200         Log.d("test", lrc + "lrc");
201         makeFile(lrc, path);
202 
203     }
204 
205     protected void makeFile(final String lrc, final String path) {
206         new Thread() {
207             public void run() {
208                 try {
209                     URL url1 = new URL(lrc);
210                     InputStream in = url1.openStream();
211                     BufferedReader reader = new BufferedReader(
212                             new InputStreamReader(in));
213                     String s1 = path.substring(0, path.length() - 4);
214                     s1 += ".lrc";
215                     int len = -1;
216                     File file = new File(s1);
217                     // FileOutputStream fout=new FileOutputStream(file);
218                     FileWriter fw = new FileWriter(file);
219                     String s = "";
220                     while ((s = reader.readLine()) != null) {
221                         fw.write(s);
222                         Log.d("test", s);
223                     }
224                     fw.close();
225                 } catch (Exception e) {
226                     // TODO Auto-generated catch block
227                     e.printStackTrace();
228                 }
229             };
230         }.start();
231     }
232 
233     protected String analysis(final String json) {
234         new Thread() {
235             public void run() {
236                 try {
237                     JSONObject jsonObject = new JSONObject(json);
238                     int count = jsonObject.getInt("count");
239                     Log.d("count", count + "");
240                     JSONArray array = jsonObject.getJSONArray("result");
241                     lrc = array.getJSONObject(0).getString("lrc");
242                     Log.d("test", "lrc  dhajdjhaj" + lrc);
243                     // 获取到lrc地址
244                 } catch (Exception e) {
245                     // TODO Auto-generated catch block
246                     e.printStackTrace();
247                 }
248             }
249         }.start();
250         return lrc;
251     }
252 
253     private String getJson(final String songname,final String singer) {
254         out = new ByteArrayOutputStream();
255         new Thread() {
256             public void run() {
257                 InputStream inStream = null;
258                 String url = null;
259                 URL urlLrc = null;
260                 try {
261                     url = setParams(songname, singer);
262                 } catch (UnsupportedEncodingException e1) {
263                     // TODO Auto-generated catch block
264                     e1.printStackTrace();
265                 }
266                 try {
267                     urlLrc = new URL(url);
268                 } catch (MalformedURLException e) {
269                     // TODO Auto-generated catch block
270                     e.printStackTrace();
271                 }
272                 Log.d("test", urlLrc + "");
273                 byte[] data = new byte[1024];
274                 int len = 0;
275                 HttpURLConnection conn = null;
276                 try {
277                     conn = (HttpURLConnection) urlLrc.openConnection();
278                 } catch (IOException e) {
279                     // TODO Auto-generated catch block
280                     e.printStackTrace();
281                 }
282                 try {
283                     conn.setRequestMethod("GET");
284                 } catch (ProtocolException e) {
285                     // TODO Auto-generated catch block
286                     e.printStackTrace();
287                 }// 使用GET方法获取
288                 conn.setConnectTimeout(5000);
289                 Log.d("test", "conn" + conn);
290                 try {
291                     conn.connect();
292                 } catch (IOException e) {
293                     // TODO Auto-generated catch block
294                     e.printStackTrace();
295                 }
296 
297                 try {
298                     inStream = conn.getInputStream();
299                 } catch (IOException e) {
300                     // TODO Auto-generated catch block
301                     e.printStackTrace();
302                 }
303 
304                 Log.d("test", inStream + "instream");
305                 try {
306                     while ((len = inStream.read(data)) != -1) {
307                         Log.d("test", data + "data");
308                         out.write(data, 0, len);
309                     }
310                 } catch (IOException e) {
311                     // TODO Auto-generated catch block
312                     e.printStackTrace();
313                 }
314                 try {
315                     out.flush();
316                 } catch (IOException e) {
317                     // TODO Auto-generated catch block
318                     e.printStackTrace();
319                 }
320                 Log.d("test", data.toString());
321                 try {
322                     inStream.close();
323                 } catch (IOException e) {
324                     // TODO Auto-generated catch block
325                     e.printStackTrace();
326                 }
327                 try {
328                     out.close();
329                 } catch (IOException e) {
330                     // TODO Auto-generated catch block
331                     e.printStackTrace();
332                 }
333                 //
334             }
335         }.start();
336         return new String(out.toByteArray());
337     }
338 
339     private String setParams(String songname, String singer)
340             throws UnsupportedEncodingException {
341         String song = URLEncoder.encode(songname, "UTF-8");
342         String sing = URLEncoder.encode(singer, "UTF-8");
343         return url + song + "/" + sing;
344     }
345 
346     private void createFile(String path, String songname, String singer) {
347 
348     }
349 
350     public int TimeStr(String timeStr) {
351 
352         timeStr = timeStr.replace(":", ".");
353         timeStr = timeStr.replace(".", "@");
354 
355         String timeData[] = timeStr.split("@");
356 
357         // 分离出分、秒并转换为整型
358         int minute = Integer.parseInt(timeData[0]);
359         int second = Integer.parseInt(timeData[1]);
360         int millisecond = Integer.parseInt(timeData[2]);
361 
362         // 计算上一行与下一行的时间转换为毫秒数
363         int currentTime = (minute * 60 + second) * 1000 + millisecond * 10;
364 
365         return currentTime;
366     }
367 
368     /**
369      * 解析歌词时间 歌词内容格式如下: [00:02.32]陈奕迅 [00:03.43]好久不见 [00:05.22]歌词制作 王涛
370      * 
371      * @param timeStr
372      * @return
373      */
374     public int time2Str(String timeStr) { // 将分:秒:毫秒转化为长整型的数
375         timeStr = timeStr.replace(":", ".");
376         timeStr = timeStr.replace(".", "@");
377 
378         String timeData[] = timeStr.split("@");
379 
380         int min = Integer.parseInt(timeData[0]);
381         int sec = Integer.parseInt(timeData[1]);
382         int millSec = Integer.parseInt(timeData[2]);
383 
384         int currentTime = (min * 60 + sec) * 1000 + millSec * 10;
385         return currentTime;
386     }
387 
388     public List<LrcContent> getLrcList() {
389         return lrcList;
390     }
391 
392 }
LrcProgress

url需要utf-8转换

否则得不到的对应的数据,既找不到对应的url

音乐播放器

标签:

原文地址:http://www.cnblogs.com/tannian/p/4567770.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!