android音乐播放器开发在线加载歌词,android开发计算器源码

该博客介绍了一个Android音乐播放器如何实现在线加载歌词的功能。通过`OnlineLrcUtil`类,从指定API获取歌词,处理网络请求,对歌词内容进行编码解码,并将其保存到本地。同时,`LrcProcess`类用于处理下载后的歌词文件,读取歌词内容,解析时间戳。示例代码展示了如何处理歌手和歌曲名,以及如何将歌词时间转换为毫秒数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. import android.util.Log;

  2. public class OnlineLrcUtil {

  3. private static String TAG = “OnlineLrcUtil”;

  4. private static OnlineLrcUtil instance;

  5. public static final String lrcRootPath = Environment

  6. .getExternalStorageDirectory().toString()

  7. + “/SweetMusicPlayer/Lyrics/”;

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

  9. public static OnlineLrcUtil getInstance() {

  10. if (null == instance) {

  11. instance = new OnlineLrcUtil();

  12. }

  13. return instance;

  14. }

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

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

  17. }

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

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

  20. try {

  21. URL url = new URL(queryLrcURLStr);

  22. URLConnection urlConnection = url.openConnection();

  23. urlConnection.connect();

  24. BufferedReader in = new BufferedReader(new InputStreamReader(

  25. urlConnection.getInputStream()));

  26. StringBuffer sb = new StringBuffer();

  27. String temp;

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

  29. sb.append(temp);

  30. }

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

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

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

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

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

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

  37. } catch (MalformedURLException e) {

  38. // TODO Auto-generated catch block

  39. e.printStackTrace();

  40. } catch (IOException e) {

  41. // TODO Auto-generated catch block

  42. e.printStackTrace();

  43. } catch (JSONException e) {

  44. // TODO Auto-generated catch block

  45. e.printStackTrace();

  46. }

  47. return null;

  48. }

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

  50. public String Encode(String str) {

  51. try {

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

  53. } catch (UnsupportedEncodingException e) {

  54. // TODO Auto-generated catch block

  55. e.printStackTrace();

  56. }

  57. return str;

  58. }

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

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

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

  62. try {

  63. URL url = new URL(urlPath);

  64. URLConnection urlConnection = url.openConnection();

  65. urlConnection.connect();

  66. HttpURLConnection httpConn = (HttpURLConnection) urlConnection;

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

  68. File file = new File(lrcRootPath);

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

  70. file.mkdirs();

  71. }

  72. BufferedReader bf = new BufferedReader(new InputStreamReader(

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

  74. PrintWriter out = new PrintWriter(new BufferedWriter(

  75. new OutputStreamWriter(new FileOutputStream(lrcPath),

  76. “utf-8”)));

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

  78. int temp = -1;

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

  80. bf.read©;

  81. out.write©;

  82. }

  83. bf.close();

  84. out.close();

  85. return true;

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

  1. }

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

  3. } catch (MalformedURLException e) {

  4. // TODO Auto-generated catch block

  5. e.printStackTrace();

  6. } catch (IOException e) {

  7. // TODO Auto-generated catch block

  8. e.printStackTrace();

  9. }

  10. return false;

  11. }

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

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

  14. }

  15. }

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) {

//替换字符

s = s.replace("[", “”);

s = s.replace("]", “@”);

//分离“@”字符

String splitLrcData[] = s.split("@");

if(splitLrcData.length > 1) {

Log.i(“INFO”, splitLrcData[1]+“歌词”);

mLrcContent.setLrcStr(splitLrcData[1]);

Log.i(“INFO”, splitLrcData[0]+“时间”);

//处理歌词取得歌曲的时间

int lrcTime = time2Str(splitLrcData[0]);

mLrcContent.setLrcTime(lrcTime);

//添加进列表数组

lrcList.add(mLrcContent);

//新创建歌词内容对象

mLrcContent = new LrcContent();

}

}

} catch (FileNotFoundException e) {

e.printStackTrace();

stringBuilder.append(“木有歌词文件,赶紧去下载!…”);

} catch (IOException e) {

e.printStackTrace();

stringBuilder.append(“木有读取到歌词哦!”);

}

return stringBuilder.toString();

}

/**

  • 解析歌词时间

  • 歌词内容格式如下:

  • [00:02.32]陈奕迅

  • [00:03.43]好久不见

  • [00:05.22]歌词制作 王涛

  • @param timeStr

  • @return

*/

public int time2Str(String timeStr) {

timeStr = timeStr.replace(":", “.”);

timeStr = timeStr.replace(".", “@”);

String timeData[] = timeStr.split("@"); //将时间分隔成字符串数组

//分离出分、秒并转换为整型

int minute = Integer.parseInt(timeData[0]);

int second = Integer.parseInt(timeData[1]);

int millisecond = Integer.parseInt(timeData[2]);

//计算上一行与下一行的时间转换为毫秒数

int currentTime = (minute * 60 + second) * 1000 + millisecond * 10;

return currentTime;

}

public List getLrcList() {

return lrcList;

}

Mainactivity 大概调用方法

public class MainActivity extends Activity {

//好久不见 青花

private String mMusicName=“夜曲”;

//陈奕迅 周传雄

private String mMusicAutor=“周杰伦”;

@SuppressLint(“SdCardPath”)

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值