android Mp3播放器之Lrc歌词解析

默认使用GBK编码解析,解析后的内容存放在HashTable中,歌词时间统一精确到s。
package com.aws.mp3;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Hashtable;

/**
 * <b>歌词解析类</b>
 * 
 * @QQ QQ:951868171
 * @version 1.0
 * @email xi_yf_001@126.com
 * */
public class LrcDecode {

	private Hashtable<String, String> lrcTable = null;	//存放解析后的列表
	public static String charSet = "gbk";		//编码
	/**
	 * 解析Lrc
	 * */
	public LrcDecode readLrc(InputStream is) {
		lrcTable = new Hashtable<String, String>();
		try {
			BufferedReader bis = new BufferedReader(new InputStreamReader(is,
					charSet));
			String str = null;
			while ((str = bis.readLine()) != null) {	//逐行解析
				decodeLine(str);		
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			lrcTable = null;
		}
		return this;
	}

	/**
	 * 单行解析
	 * */
	private LrcDecode decodeLine(String str) {

		if (str.startsWith("[ti:")) {// 歌曲名
			lrcTable.put("ti", str.substring(4, str.lastIndexOf("]")));

		} else if (str.startsWith("[ar:")) {// 艺术家
			lrcTable.put("ar", str.substring(4, str.lastIndexOf("]")));

		} else if (str.startsWith("[al:")) {// 专辑
			lrcTable.put("al", str.substring(4, str.lastIndexOf("]")));

		} else if (str.startsWith("[by:")) {// 作词
			lrcTable.put("by", str.substring(4, str.lastIndexOf("]")));

		} else if (str.startsWith("[la:")) {// 语言
			lrcTable.put("la", str.substring(4, str.lastIndexOf("]")));

		} else {
			// 歌词正文
			int startIndex = -1;
			//
			while ((startIndex = str.indexOf("[", startIndex + 1)) != -1) {
				int endIndex = str.indexOf("]", startIndex + 1);
				// 添加时间格式m:ss
				lrcTable.put(strToLongToTime(str.substring(startIndex + 1,
						endIndex))
						+ "", str.substring(str.lastIndexOf("]") + 1, str
						.length()));
			}
		}
		return this;
	}

	/**
	 * 获取解析成功的歌词
	 * */
	public Hashtable<String, String> getLrcTable() {
		return lrcTable;
	}

	/**
	 * 保证时间格式一致 为m:ss
	 * 
	 * @param str
	 *            时间字符
	 * @return 判断用的时间符
	 * */
	private String strToLongToTime(String str) {
		// System.out.println(str);
		int m = Integer.parseInt(str.substring(0, str.indexOf(":")));
		int s = 0;
		int ms = 0;

		// 判断歌词时间是否有毫秒
		if (str.indexOf(".") != -1) {
			s = Integer.parseInt(str.substring(str.indexOf(":") + 1, str
					.indexOf(".")));
			ms = Integer.parseInt(str.substring(str.indexOf(".") + 1, str
					.length()));
		} else {
			s = Integer.parseInt(str.substring(str.indexOf(":") + 1, str
					.length()));
		}
		return timeMode(m * 60000 + s * 1000 + ms * 10);
	}

	/**
	 * 返回时间
	 * 
	 * @param time
	 *            毫秒时间
	 * */
	public static String timeMode(int time) {
		int tmp = (time / 1000) % 60;
		if (tmp < 10)
			return time / 60000 + ":" + "0" + tmp;
		else
			return time / 60000 + ":" + tmp;
	}

}

下载链接http://download.csdn.net/source/3553639

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Android Studio中显示LRC歌词文件,可以按照以下步骤进行操作: 1.在布局文件中添加一个TextView,用于显示歌词。 2.在Java代码中读取LRC文件并将其解析为Map,其中键为时间戳,值为歌词文本。 3.在MediaPlayer的OnPreparedListener中启动一个线程,该线程将定期更新TextView以显示当前播放时间的歌词。 下面是一个简单的示例代码,可以帮助你实现这个功能: ```java // 在布局文件中添加一个TextView,用于显示歌词 TextView lrcTextView = findViewById(R.id.lrcTextView); // 在Java代码中读取LRC文件并将其解析为Map Map<Long, String> lrcMap = new HashMap<>(); try { BufferedReader br = new BufferedReader(new FileReader(lrcFile)); String line; while ((line = br.readLine()) ! null) { Matcher matcher = Pattern.compile("\\[(\\d{2}):(\\d{2})\\.(\\d{2})\\](.*)").matcher(line); if (matcher.find()) { long time = Long.parseLong(matcher.group(1)) * 60 * 1000 + Long.parseLong(matcher.group(2)) * 1000 + Long.parseLong(matcher.group(3)) * 10; String text = matcher.group(4); lrcMap.put(time, text); } } br.close(); } catch (IOException e) { e.printStackTrace(); } // 在MediaPlayer的OnPreparedListener中启动一个线程,该线程将定期更新TextView以显示当前播放时间的歌词 MediaPlayer mediaPlayer = new MediaPlayer(); mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { new Thread(new Runnable() { @Override public void run() { while (mediaPlayer.isPlaying()) { long time = mediaPlayer.getCurrentPosition(); if (lrcMap.containsKey(time)) { runOnUiThread(new Runnable() { @Override public void run() { lrcTextView.setText(lrcMap.get(time)); } }); } try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); } }); // 播放音乐 mediaPlayer.setDataSource(audioFile); mediaPlayer.prepare(); mediaPlayer.start(); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值