Java LRC歌詞解析

7739 ワード

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("
"); 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; } }

mp 3のプレーヤーをする時1つのLRCの歌詞の解析のツールを書いて、みんなと分かち合って、足りないところがあって、みんなが指摘することを望んで、テストの歌詞
[ti:将军令][ar:五月天][al:映画『黄飞鸿』主题曲][by:45553904][00:00.00]将军令[00:01.76][00:06.64]映画『黄飞鸿』主题曲[00:04.15]作词:阿信[00:05.61]作曲:阿信[00:07.12]唱:五月天[00:13.59][00:21.45]だれかの命令を待って[00:24.80]あなたの自由を思い出す[00:27.24]いつからこのような生活に慣れているのかOh~[00:29.88][00:32.28]何を信じているのか[00:34.66]実はあなたはとっくに決められている[00:37.13]あなたは我慢しているがOhを受け入れたくない[00:47.75][00:46.66]歴史は勝者の手に落ちている[00:44.53]少なくとも私たちは伝説を持っている[00:46.90]誰が敗者が不朽ではないと言っているのか[00:47.51]拳は頭を[00:54.46]359.梦を叶えてあげるよ[00:56.72]梦を见上げて追いかけてゆくよ[01:01.34][01:04.57]人生の终わりまで君は谁だったんだろう[01:08.24]彼らは言った[01:13.80]命は长い风が谁かの心を吹き飞ばしたようだ[01:19.13]君が覚えておきたいその名前は何だったんだろう[01:23.20][01:52.2.21]大时代君も仆もカゲロウだった[01:27.24]昨日までは顽童だったのに[01:29.70]今日は肩が重いOh~[01:34.27][01:34.62]歌がただ流れていた[01:36.93]宇宙が谁かに见えるようになった[01:39.47]何もかも君に闻こえるOh~[01:42.43]星月はずっと黙っていた[01:47.08]微光无力に隅っこを照らす[01:49.33]君の瞳を灯してほしい[01:54.53]01:54.24.戦场はあなたを见逃さない[01:56.67]人々が自分を目覚めさせるまで[01:54.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]土地を失って空を失ったあなたが覚えておきたいその名前は何ですか[03:19.34]あなたは何を信じますか?あなたは何に执着しますか?あなたは何ですか[03:22.95][03:24.36]私は歌词网:www.5 ilrc.com[03:26.17]歌词编辑:宅之轩[03:28.93]QQ:4553904[03:32.87][03:35.63]
modify at 2014-12-1
使用中、中国語の歌詞をロードできないことがわかりました.コードの問題が原因で、今修正して、ファイルの入力ストリームの1行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();
		}
	}