0
点赞
收藏
分享

微信扫一扫

android音乐播放器开发在线加载歌词

愚鱼看书说故事 2022-04-24 阅读 109

1)通过地址查询出歌词的地址。(这里楼主用URLConnection)

2)通过歌词地址缓冲歌词文件。(这里楼主用URLConnection)

3)加载缓冲好的歌词文件。

上面说的看起来还是比较容易,楼主自己写了个demo,是一个[Java](()工程,发现没啥问题,正常加载歌词文件。

等到[Android](()上,第一步就跪了。发现URLConnection的getInputStream()抛出一个io异常,简直要命,折腾了半天才发现是因为带了[http://gec 《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》无偿开源 徽信搜索公众号【编程进阶路】 i.me/api/lyric/安静/周杰伦](()中文路径。由于默认是gbk,网络传输为utf-8,所以要把中文转码,URLEncoder.encode(str,“utf-8”);即可。

到了第2步,问题又出现了,歌词乱码。解决办法,用字符流操作比较合适,还要注意同一编码。

[java] [view plain](() [copy](()

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NPMunut2-1650775496168)(https://code.csdn.net/assets/CODE_ico.png)][外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-z8yBkxu7-1650775496169)(https://code.csdn.net/assets/ico_fork.svg)]

  1. package com.huwei.sweetmusicplayer.util;

  2. import java.io.BufferedReader;

  3. import java.io.BufferedWriter;

  4. import java.io.File;

  5. import java.io.FileOutputStream;

  6. import java.io.IOException;

  7. import java.io.InputStreamReader;

  8. import java.io.OutputStreamWriter;

  9. import java.io.PrintWriter;

  10. import java.io.UnsupportedEncodingException;

  11. import java.net.HttpURLConnection;

  12. import java.net.MalformedURLException;

  13. import java.net.URL;

  14. import java.net.URLConnection;

  15. import java.net.URLEncoder;

  16. import java.util.Random;

  17. import org.json.JSONArray;

  18. import org.json.JSONException;

  19. import org.json.JSONObject;

  20. import android.os.Environment;

  21. import android.util.Log;

  22. public class OnlineLrcUtil {

  23. private static String TAG = “OnlineLrcUtil”;

  24. private static OnlineLrcUtil instance;

  25. public static final String lrcRootPath = Environment

  26. .getExternalStorageDirectory().toString()

  27. + “/SweetMusicPlayer/Lyrics/”;

  28. public static final String queryLrcURLRoot = “http://geci.me/api/lyric/”;

  29. public static OnlineLrcUtil getInstance() {

  30. if (null == instance) {

  31. instance = new OnlineLrcUtil();

  32. }

  33. return instance;

  34. }

  35. public String getQueryLrcURL(String title, String artist) {

  36. return queryLrcURLRoot + Encode(title) + “/” + Encode(artist);

  37. }

  38. public String getLrcURL(String title, String artist) {

  39. String queryLrcURLStr = getQueryLrcURL(title, artist);

  40. try {

  41. URL url = new URL(queryLrcURLStr);

  42. URLConnection urlConnection = url.openConnection();

  43. urlConnection.connect();

  44. BufferedReader in = new BufferedReader(new InputStreamReader(

  45. urlConnection.getInputStream()));

  46. StringBuffer sb = new StringBuffer();

  47. String temp;

  48. while ((temp = in.readLine()) != null) {

  49. sb.append(temp);

  50. }

  51. JSONObject jObject = new JSONObject(sb.toString());

  52. int count = jObject.getInt(“count”);

  53. int index = count == 0 ? 0 : new Random().nextInt() % count;

  54. JSONArray jArray = jObject.getJSONArray(“result”);

  55. JSONObject obj = jArray.getJSONObject(index);

  56. return obj.getString(“lrc”);

  57. } catch (MalformedURLException e) {

  58. // TODO Auto-generated catch block

  59. e.printStackTrace();

  60. } catch (IOException e) {

  61. // TODO Auto-generated catch block

  62. e.printStackTrace();

  63. } catch (JSONException e) {

  64. // TODO Auto-generated catch block

  65. e.printStackTrace();

  66. }

  67. return null;

  68. }

  69. // 歌手,歌曲名中的空格进行转码

  70. public String Encode(String str) {

  71. try {

  72. return URLEncoder.encode(str.trim(), “utf-8”);

  73. } catch (UnsupportedEncodingException e) {

  74. // TODO Auto-generated catch block

  75. e.printStackTrace();

  76. }

  77. return str;

  78. }

  79. // 歌词文件网络地址,歌词文件本地缓冲地址

  80. public boolean wrtieContentFromUrl(String urlPath, String lrcPath) {

  81. Log.i(TAG, “lrcURL” + urlPath);

  82. try {

  83. URL url = new URL(urlPath);

  84. URLConnection urlConnection = url.openConnection();

  85. urlConnection.connect();

  86. HttpURLConnection httpConn = (HttpURLConnection) urlConnection;

  87. if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {

  88. File file = new File(lrcRootPath);

  89. if (!file.exists()) {

  90. file.mkdirs();

  91. }

  92. BufferedReader bf = new BufferedReader(new InputStreamReader(

  93. urlConnection.getInputStream(), “utf-8”));

  94. PrintWriter out = new PrintWriter(new BufferedWriter(

  95. new OutputStreamWriter(new FileOutputStream(lrcPath),

  96. “utf-8”)));

  97. char c[] = new char[256];

  98. int temp = -1;

  99. while ((temp = bf.read()) != -1) {

  100. bf.read©;

  101. out.write©;

  102. }

  103. bf.close();

  104. out.close();

  105. return true;

  106. }

  107. // System.out.println(“getFile:”+str);

  108. } catch (MalformedURLException e) {

  109. // TODO Auto-generated catch block

  110. e.printStackTrace();

  111. } catch (IOException e) {

  112. // TODO Auto-generated catch block

  113. e.printStackTrace();

  114. }

  115. return false;

  116. }

  117. public String getLrcPath(String title, String artist) {

  118. return lrcRootPath + title + " - " + artist + “.lrc”;

  119. }

  120. }

LrcProcess 类,用来保存处理下载后的文件

public class LrcProcess {

private List lrcList; //List集合存放歌词内容对象

private LrcContent mLrcContent; //声明一个歌词内容对象

/**

  • 无参构造函数用来实例化对象

*/

public LrcProcess() {

mLrcContent = new LrcContent();

lrcList = new ArrayList();

}

/**

  • 读取歌词

  • @param path

  • @return

*/

public String readLRC(String path) {

//定义一个StringBuilder对象,用来存放歌词内容

StringBuilder stringBuilder = new StringBuilder();

File f = new File(path.replace(“.mp3”, “.lrc”));

try {

//创建一个文件输入流对象

FileInputStream fis = new FileInputStream(f);

InputStreamReader isr = new InputStreamReader(fis, “utf-8”);

BufferedReader br = new BufferedReader(isr);

String s = “”;

while((s = br.readLine()) != null) {

tring path) {

//定义一个StringBuilder对象,用来存放歌词内容

StringBuilder stringBuilder = new StringBuilder();

File f = new File(path.replace(“.mp3”, “.lrc”));

try {

//创建一个文件输入流对象

FileInputStream fis = new FileInputStream(f);

InputStreamReader isr = new InputStreamReader(fis, “utf-8”);

BufferedReader br = new BufferedReader(isr);

String s = “”;

while((s = br.readLine()) != null) {

举报

相关推荐

0 条评论