Fix loading LRC files with regex control characters in name

Fix non-standart LRC timestamps
This commit is contained in:
tkashkin 2017-06-14 18:49:11 +03:00
commit 1a71950930
3 changed files with 44 additions and 27 deletions

View file

@ -4,25 +4,32 @@ import android.util.SparseArray;
public abstract class AbsSynchronizedLyrics {
public final SparseArray<String> lines = new SparseArray<>();
public boolean isValid = false;
public static AbsSynchronizedLyrics parse(String data)
{
return new SynchronizedLyricsLRC(data); // no another formats at the moment
public static AbsSynchronizedLyrics parse(String data, boolean justCheck) {
return new SynchronizedLyricsLRC(data, justCheck); // no another formats at the moment
}
public String getLine(int time)
{
public static AbsSynchronizedLyrics parse(String data) {
return parse(data, false);
}
public static boolean isSynchronized(String data) {
AbsSynchronizedLyrics lyrics = parse(data, true);
return lyrics.isValid;
}
public String getLine(int time) {
time += 500; // small time adjustment to display line before it actually starts
int lastLineTime = lines.keyAt(0);
for(int i = 0; i < lines.size(); i++) {
for (int i = 0; i < lines.size(); i++) {
int lineTime = lines.keyAt(i);
if(time >= lineTime) {
if (time >= lineTime) {
lastLineTime = lineTime;
}
else {
} else {
break;
}
}

View file

@ -5,38 +5,40 @@ import java.util.regex.Pattern;
public class SynchronizedLyricsLRC extends AbsSynchronizedLyrics {
private static Pattern LRC_LINE_PATTERN = Pattern.compile("((?:\\[.*?\\])+)(.*)");
private static Pattern LRC_TIME_PATTERN = Pattern.compile("\\[(\\d\\d):(\\d\\d)(?:\\.(\\d\\d))\\]");
private static Pattern LRC_TIME_PATTERN = Pattern.compile("\\[(\\d+):(\\d{2}(?:\\.\\d+)?)\\]");
public SynchronizedLyricsLRC(String data)
{
if(data == null || data.isEmpty()) {
public SynchronizedLyricsLRC(String data, boolean justCheck) {
if (data == null || data.isEmpty()) {
return;
}
String[] lines = data.split("\r?\n");
for(String line : lines) {
for (String line : lines) {
line = line.trim();
if(line.isEmpty()) {
if (line.isEmpty()) {
continue;
}
Matcher matcher = SynchronizedLyricsLRC.LRC_LINE_PATTERN.matcher(line);
if(matcher.find()) {
if (matcher.find()) {
String time = matcher.group(1);
String text = matcher.group(2);
Matcher timeMatcher = SynchronizedLyricsLRC.LRC_TIME_PATTERN.matcher(time);
while(timeMatcher.find()) {
int m = 0, s = 0, x = 0;
while (timeMatcher.find()) {
int m = 0;
float s = 0f;
try {
m = Integer.parseInt(timeMatcher.group(1));
s = Integer.parseInt(timeMatcher.group(2));
x = Integer.parseInt(timeMatcher.group(3));
s = Float.parseFloat(timeMatcher.group(2));
} catch (NumberFormatException ex) {
ex.printStackTrace();
}
int ms = x*10 + s*1000 + m*60000;
int ms = (int) (s * 1000f) + m * 60000;
this.isValid = true;
if (justCheck) return;
this.lines.append(ms, text);
}