为什么做lrc歌词呢,其他精确的qrc之类的都是加密的,读取出来是乱码,参照了https://blog.csdn.net/chuan403082010/article/details/61912873
以下是效果,具体没对上的应该是lrc歌词的问题
http://v.youku.com/v_show/id_XMzUyMzIxNDIzNg==.html?spm=a2h3j.8428770.3416059.1
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
public class ReadLRC : MonoBehaviour
{
private Text lrcText;
public float offest = 0.1f;
private void Start()
{
lrcText = GetComponent<Text>();
GetLrcFile(Application.streamingAssetsPath + "/邓紫棋-盲点.lrc");
}
public List<string> GetLyricListAndTimeList(string lyricText, out List<float> timeA, out List<string> titleA)
{
List<string> lyricArray = new List<string>();
timeA = new List<float>();
titleA = new List<string>();
string[] lineArray = lyricText.Split('\n');//根据分隔出行
for (int i = 0; i < lineArray.Length; i++)
{
string lineStr = lineArray[i];
if (lineStr.Contains("ti") || lineStr.Contains("ar") || lineStr.Contains("al") || lineStr.Contains("by") || lineStr.Contains("offset"))
{//标题
string[] array = lineStr.Split('[', ':', ']');
float f;
if (!float.TryParse(array[array.Length - 2], out f) && array[array.Length - 2] != null)
{
titleA.Add(array[array.Length - 2]);
}
}
else
{//歌词
string[] contentArray = lineStr.Split('[', ']');
for (int j = contentArray.Length - 1; j >= 0; j--)
{
string subStr = contentArray[j];
string newSubStr = subStr.Replace(":", "");
float temp;
if (float.TryParse(newSubStr, out temp))
{
string[] time = subStr.Split(':');
float min;
float.TryParse(time[0], out min);
float sec;
float.TryParse(time[1], out sec);
subStr = string.Format("{0}", (sec + 60 * min));
}
float num = 0f;
if (float.TryParse(subStr, out num))
{
timeA.Add(num);
if (float.TryParse(contentArray[contentArray.Length - 1], out num))
{
lyricArray.Add("");
}
else
{
lyricArray.Add(contentArray[contentArray.Length - 1]);
}
}
}
}
}
return lyricArray;
}
public List<string> SortLyricListAndTimeList(List<string> lyricA, List<float> timeA, out List<float> timeArray)
{
for (int i = 0; i < timeA.Count - 1; i++)
{
for (int j = 0; j < timeA.Count - 1 - i; j++)
{
if (timeA[j] > timeA[j + 1])
{
float temp = timeA[j];
timeA[j] = timeA[j + 1];
timeA[j + 1] = temp;
string tempLyric = lyricA[j];
lyricA[j] = lyricA[j + 1];
lyricA[j + 1] = tempLyric;
}
}
}
timeArray = timeA;
return lyricA;
}
/*
[ti:盲点] 1
[ar:G.E.M. 邓紫棋]2
[al:新的心跳]3
[t_time:(04:36)] 4
[00:00.50]盲点 - G.E.M. 邓紫棋
[00:03.40]词:G.E.M. 邓紫棋
[00:05.55]曲:G.E.M. 邓紫棋
[00:07.45]编曲:Lupo Groinig
[00:09.25]监制:Lupo Groinig
[00:13.70]我数着脚步的声音
[00:20.57]漫无目的地前进
[00:26.50]回家或到处散散心
[00:32.90]我也做不了决定
[00:39.40]生活事业社交关系
[00:45.90]明明都努力上进
[00:52.50]但望着繁忙行事历
[00:57.65]只感觉到空洞的心
[01:03.90]世界我看得再远
*/
//读取lrc歌词文件
public void GetLrcFile(string file)
{
StreamReader sr = new StreamReader(file, Encoding.Default);
string str = sr.ReadToEnd();
List<float> a = new List<float>(); //取得了时间点
List<string> b = new List<string>(); //多少行标题
List<float> c = new List<float>();
List<string> d = new List<string>();//歌词
List<string> e = new List<string>();
d = GetLyricListAndTimeList(str, out a, out b); //输出了时间点和 歌词list
e = SortLyricListAndTimeList(d, a, out c);//得到了每行歌词 和时间点
//这里很乱,我是先达到具体目的,优化以后再考虑
StartCoroutine(ShowLrc(e, c));
}
IEnumerator ShowLrc(List<string> lrc, List<float> t)
{
lrcText.text = lrc[0];
yield return new WaitForSeconds(t[0]);
for (int i = 1; i < lrc.Count; i++)
{
float ts = t[i] - t[i - 1]-offest; //偏移量
yield return new WaitForSeconds(ts);
lrcText.text = lrc[i];
}
}
}
2020年3月5日
有网友反映乱码,那是因为unity的中文 编码必须为UTF-8,在qq音乐下载的lrc歌词需要另存为一下
@择一城终老_1557
处理乱码这是基础知识吧
网友评论