Java LRC 歌词解析

public class LrcAnalyze {

	/**
	 * [ar:艺人名] [ti:曲名] [al:专辑名] [by:编者(指编辑LRC歌词的人)] [offset:时间补偿值]
	 * 其单位是毫秒,正值表示整体提前,负值相反。这是用于总体调整显示快慢的。
	 * */
	// parse taget artist
	private final String TagAr = "[ar:";

	// perse taget tittle
	private final String TagTi = "[ti:";

	// perse target album
	private final String TagAl = "[al:";

	// perse target author of the lrc
	private final String TagBy = "[by:";

	// perse taget offset
	private final String TagOff = "[offset:";

	// record the file
	private FileInputStream filein;

	// record the file
	private File file;

	// get lrc artist
	public static final int ARTIST_ZONE = 0;

	// get lrc tittle
	public static final int TITTLE_ZONE = 1;

	// get lrc album
	public static final int ALBUM_ZONE = 2;

	// get lrc author
	public static final int AOTHOR_ZONE = 3;

	// get lrc offset
	public static final int OFFSET_ZONE = 4;

	// get lrc
	public static final int LRC_ZONE = 5;

	// lrc data contract
	public class LrcData {
		public int type;
		public String Time; // time of string format
		public long TimeMs; // time of long format ms
		// public char TimeHour; // hour of time
		// public char TimeMinute; // minute of time
		// public char TimeSecond; // second of time
		// public char TimeMilliSecond; // millisecond of time
		public String LrcLine; // one line lrc
	}

	// record analyzed lrc
	private List LrcList;

	/**
	 * constract
	 * */
	public LrcAnalyze(File file) {
		try {
			filein = new FileInputStream(file);

			this.file = file;

			LrcList = new ArrayList();

			LrcAnalyzeStart();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * constract
	 * */
	public LrcAnalyze(String path) {
		try {
			filein = new FileInputStream(path);

			file = new File(path);

			LrcList = new ArrayList();

			LrcAnalyzeStart();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private long LrcAnalyzeTimeStringToValue(String time) {
		// System.out.println(time.substring(0, time.lastIndexOf(":")));
		// System.out.println(time.substring(time.indexOf(":") + 1,
		// time.lastIndexOf(".")));
		// System.out.println(time.substring(time.indexOf(".") + 1));

		long minute = Integer
				.parseInt(time.substring(0, time.lastIndexOf(":")));

		long second = Integer.parseInt(time.substring(time.indexOf(":") + 1,
				time.lastIndexOf(".")));

		long millisecond = Integer
				.parseInt(time.substring(time.indexOf(".") + 1));

		return (long) (minute * 60 * 1000 + second * 1000 + millisecond);
	}

	private void LrcAnalyzeLine(String ContentLine) {

		if (ContentLine.indexOf(TagAr) != -1) {// whether artist or not
			LrcData lrcdata = new LrcData();
			lrcdata.type = ARTIST_ZONE;
			lrcdata.LrcLine = ContentLine.substring(
					ContentLine.indexOf(':') + 1, ContentLine.lastIndexOf(']'));
			// System.out.println(lrcline.LrcLine);
			LrcList.add(lrcdata);
		} else if (ContentLine.indexOf(TagAl) != -1) {// whether album or not
			LrcData lrcdata = new LrcData();
			lrcdata.type = ALBUM_ZONE;
			lrcdata.LrcLine = ContentLine.substring(
					ContentLine.indexOf(':') + 1, ContentLine.lastIndexOf(']'));
			// System.out.println(lrcline.LrcLine);
			LrcList.add(lrcdata);
		} else if (ContentLine.indexOf(TagTi) != -1) {// whether tittle or not
			LrcData lrcdata = new LrcData();
			lrcdata.type = TITTLE_ZONE;
			lrcdata.LrcLine = ContentLine.substring(
					ContentLine.indexOf(':') + 1, ContentLine.lastIndexOf(']'));
			// System.out.println(lrcline.LrcLine);
			LrcList.add(lrcdata);
		} else if (ContentLine.indexOf(TagBy) != -1) {// whether author or not
			LrcData lrcdata = new LrcData();
			lrcdata.type = AOTHOR_ZONE;
			lrcdata.LrcLine = ContentLine.substring(
					ContentLine.indexOf(':') + 1, ContentLine.lastIndexOf(']'));
			// System.out.println(lrcline.LrcLine);
			LrcList.add(lrcdata);
		} else if (ContentLine.indexOf(TagOff) != -1) {// whether offset or not
			LrcData lrcdata = new LrcData();
			lrcdata.type = OFFSET_ZONE;
			lrcdata.LrcLine = ContentLine.substring(
					ContentLine.indexOf(':') + 1, ContentLine.lastIndexOf(']'));
			// System.out.println(lrcline.LrcLine);
			LrcList.add(lrcdata);
		} else {// lrc content
			String[] cut = ContentLine.split("]");
			if (cut.length >= 2) {
				for (int i = 0; i < cut.length - 1; i++) {
					LrcData lrcdata = new LrcData();
					lrcdata.type = LRC_ZONE;
					lrcdata.Time = cut[i]
							.substring(ContentLine.indexOf('[') + 1);
					lrcdata.TimeMs = LrcAnalyzeTimeStringToValue(lrcdata.Time);
					lrcdata.LrcLine = cut[cut.length - 1];
					// System.out.println("------" + i + "-----"
					// + ">>>>>>>" + lrcdata.Time
					// + ">>>>>>>" + lrcdata.LrcLine
					// + ">>>>>>>" + lrcdata.TimeMs);
					LrcList.add(lrcdata);
				}
			}
		}
	}

	private void LrcAnalyzeStart() {
		try {
			// new memory for file content
			byte[] ContentForByte = new byte[(int) file.length()];

			// read file content
			filein.read(ContentForByte);

			// cover byte to string
			String ContentForString = new String(ContentForByte);

			String[] ContentLine = ContentForString.split("\n");

			for (int i = 0; i < ContentLine.length; i++) {
				// System.out.println(ContentLine[i]);
				LrcAnalyzeLine(ContentLine[i]);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public List LrcGetList() {
		return LrcList;
	}
}



在做mp3播放器的时候写了一个LRC的歌词解析的工具,和大家分享一下,有不足的地方,希望大家指出,测试的歌词

[ti:将军令]
[ar:五月天]
[al:电影《黄飞鸿》主题曲]
[by:45553904]
[00:00.00]将军令
[00:01.76]
[00:02.64]电影《黄飞鸿》主题曲
[00:04.15]作词:阿信
[00:05.61]作曲:阿信
[00:07.12]演唱:五月天
[00:13.59]
[00:22.45]在等谁 一声下令以後
[00:24.80]才想起 呼吸你的自由
[00:27.24]从何时 习惯这种生活 Oh ~
[00:29.88]
[00:32.28]不相信 或是相信什麽
[00:34.66]其实你 早已被决定过
[00:37.13]你忍受 但是不愿接受 Oh ~
[00:40.75]
[00:41.66]历史 落在 赢家 之手
[00:44.53]至少 我们 拥有 传说
[00:46.90]谁说 败者 无法 不朽
[00:49.51]
[00:51.56]拳头 只能 让人 低头
[00:54.46]念头 却能 让人 抬头
[00:56.72]抬头 去看 去爱 去追 你心中的梦
[01:01.34]
[01:04.57]此生到尽头 你是谁 曾怎麽活
[01:09.24]他们说 就让他们去说
[01:13.80]生命如长风 吹过谁 的心头
[01:19.13]你想被记住 的那个名字 将会是什麽
[01:23.20]
[01:25.21]大时代 你我都是蜉蝣
[01:27.24]在昨天 你我还是顽童
[01:29.70]而今天 双肩如此沈重 Oh ~
[01:33.27]
[01:34.62]一首歌 只是靡靡唱游
[01:36.93]或者能 让谁看见宇宙
[01:39.47]全看你 愿意听见什麽 Oh ~
[01:42.43]
[01:44.53]星月 从来 只能 沉默
[01:47.08]微光 无力 遍照 角落
[01:49.33]只求 点亮 你的 瞳孔
[01:51.53]
[01:54.24]战场 不会 放过 你我
[01:56.67]直到 人们 觉醒 自我
[01:59.08]何时 不盼 不求 不等 将军或英雄
[02:03.74]
[02:09.42]此生到尽头 你是谁 曾怎麽活
[02:14.10]他们说 就让他们去说
[02:18.84]生命如长风 吹过谁 的心头
[02:24.15]你想被记住 的那个名字 将会是什麽
[02:27.95]
[02:49.05]苍空盼飞鸿 苍生等英雄
[02:51.17]我们颠沛千年依然还在等候
[02:53.68]失去了土地 失去了天空
[02:56.06]自问 不能失去什麽
[02:58.00]
[02:59.69]此刻到永久 你是谁 要怎麽活
[03:04.23]为什麽 还在问为什麽
[03:09.04]生命如长风 风中谁 在问我
[03:14.33]你想被记住 的那个名字 将会是什麽
[03:19.34]你相信什麽 你执着什麽 你就是什麽
[03:22.95]
[03:24.36]我爱歌词网:www.5ilrc.com
[03:26.17]歌词编辑:宅之轩
[03:28.93]QQ:45553904
[03:32.87]
[03:35.63]

modify at 2014-12-1

在使用过程中发现,不能加载中文歌词,原因是编码问题,现在修改,将文件的输入流一行一行的读出,并且转化成UTF-8,可以解决这个问题。

贴出LrcAnalyzeStart方法:

private void LrcAnalyzeStart() {
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(filein, "UTF-8"));
			String ContentLine;
			while((ContentLine = br.readLine()) != null){
				// System.out.println(ContentLine[i]);
				LrcAnalyzeLine(ContentLine);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


你可能感兴趣的:(android)