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
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值