Add lyrics offset time support
Some refactoring and formatting
This commit is contained in:
parent
1a71950930
commit
3122923d5f
3 changed files with 59 additions and 30 deletions
|
|
@ -3,9 +3,17 @@ package com.kabouzeid.gramophone.model.lyrics;
|
||||||
import android.util.SparseArray;
|
import android.util.SparseArray;
|
||||||
|
|
||||||
public abstract class AbsSynchronizedLyrics {
|
public abstract class AbsSynchronizedLyrics {
|
||||||
|
private static final int TIME_OFFSET_MS = 500; // time adjustment to display line before it actually starts
|
||||||
|
|
||||||
public final SparseArray<String> lines = new SparseArray<>();
|
public final SparseArray<String> lines = new SparseArray<>();
|
||||||
public boolean isValid = false;
|
public boolean isValid = false;
|
||||||
|
public int offset = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param data Lyrics string
|
||||||
|
* @param justCheck Set isValid = true and stop parsing if lyrics appears to be valid
|
||||||
|
* and has at least 1 line
|
||||||
|
*/
|
||||||
public static AbsSynchronizedLyrics parse(String data, boolean justCheck) {
|
public static AbsSynchronizedLyrics parse(String data, boolean justCheck) {
|
||||||
return new SynchronizedLyricsLRC(data, justCheck); // no another formats at the moment
|
return new SynchronizedLyricsLRC(data, justCheck); // no another formats at the moment
|
||||||
}
|
}
|
||||||
|
|
@ -20,7 +28,7 @@ public abstract class AbsSynchronizedLyrics {
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getLine(int time) {
|
public String getLine(int time) {
|
||||||
time += 500; // small time adjustment to display line before it actually starts
|
time += offset + AbsSynchronizedLyrics.TIME_OFFSET_MS;
|
||||||
|
|
||||||
int lastLineTime = lines.keyAt(0);
|
int lastLineTime = lines.keyAt(0);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,9 +4,18 @@ import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
public class SynchronizedLyricsLRC extends AbsSynchronizedLyrics {
|
public class SynchronizedLyricsLRC extends AbsSynchronizedLyrics {
|
||||||
private static Pattern LRC_LINE_PATTERN = Pattern.compile("((?:\\[.*?\\])+)(.*)");
|
private static final Pattern LRC_LINE_PATTERN = Pattern.compile("((?:\\[.*?\\])+)(.*)");
|
||||||
private static Pattern LRC_TIME_PATTERN = Pattern.compile("\\[(\\d+):(\\d{2}(?:\\.\\d+)?)\\]");
|
private static final Pattern LRC_TIME_PATTERN = Pattern.compile("\\[(\\d+):(\\d{2}(?:\\.\\d+)?)\\]");
|
||||||
|
private static final Pattern LRC_ATTRIBUTE_PATTERN = Pattern.compile("\\[(\\D+):(.+)\\]");
|
||||||
|
|
||||||
|
private static final float LRC_SECONDS_TO_MS_MULTIPLIER = 1000f;
|
||||||
|
private static final int LRC_MINUTES_TO_MS_MULTIPLIER = 60000;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param data Lyrics string
|
||||||
|
* @param justCheck Set isValid = true and stop parsing if lyrics appears to be valid
|
||||||
|
* and has at least 1 line
|
||||||
|
*/
|
||||||
public SynchronizedLyricsLRC(String data, boolean justCheck) {
|
public SynchronizedLyricsLRC(String data, boolean justCheck) {
|
||||||
if (data == null || data.isEmpty()) {
|
if (data == null || data.isEmpty()) {
|
||||||
return;
|
return;
|
||||||
|
|
@ -20,6 +29,20 @@ public class SynchronizedLyricsLRC extends AbsSynchronizedLyrics {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Matcher attrMatcher = SynchronizedLyricsLRC.LRC_ATTRIBUTE_PATTERN.matcher(line);
|
||||||
|
if (attrMatcher.find()) {
|
||||||
|
try {
|
||||||
|
String attr = attrMatcher.group(1).toLowerCase().trim();
|
||||||
|
String value = attrMatcher.group(2).toLowerCase().trim();
|
||||||
|
switch (attr) {
|
||||||
|
case "offset":
|
||||||
|
this.offset = Integer.parseInt(value);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} catch (Exception ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
Matcher matcher = SynchronizedLyricsLRC.LRC_LINE_PATTERN.matcher(line);
|
Matcher matcher = SynchronizedLyricsLRC.LRC_LINE_PATTERN.matcher(line);
|
||||||
if (matcher.find()) {
|
if (matcher.find()) {
|
||||||
String time = matcher.group(1);
|
String time = matcher.group(1);
|
||||||
|
|
@ -35,7 +58,7 @@ public class SynchronizedLyricsLRC extends AbsSynchronizedLyrics {
|
||||||
} catch (NumberFormatException ex) {
|
} catch (NumberFormatException ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
int ms = (int) (s * 1000f) + m * 60000;
|
int ms = (int) (s * LRC_SECONDS_TO_MS_MULTIPLIER) + m * LRC_MINUTES_TO_MS_MULTIPLIER;
|
||||||
|
|
||||||
this.isValid = true;
|
this.isValid = true;
|
||||||
if (justCheck) return;
|
if (justCheck) return;
|
||||||
|
|
@ -45,5 +68,6 @@ public class SynchronizedLyricsLRC extends AbsSynchronizedLyrics {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -91,8 +91,8 @@ public class PlayerAlbumCoverFragment extends AbsMusicServiceFragment implements
|
||||||
public void onDestroyView() {
|
public void onDestroyView() {
|
||||||
super.onDestroyView();
|
super.onDestroyView();
|
||||||
viewPager.removeOnPageChangeListener(this);
|
viewPager.removeOnPageChangeListener(this);
|
||||||
unbinder.unbind();
|
|
||||||
progressViewUpdateHelper.stop();
|
progressViewUpdateHelper.stop();
|
||||||
|
unbinder.unbind();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -181,10 +181,8 @@ public class PlayerAlbumCoverFragment extends AbsMusicServiceFragment implements
|
||||||
.start();
|
.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSynchronizedLyrics(AbsSynchronizedLyrics sLyrics)
|
public void setSynchronizedLyrics(AbsSynchronizedLyrics sLyrics) {
|
||||||
{
|
if (sLyrics == null || sLyrics.lines.size() == 0) {
|
||||||
if(sLyrics == null || sLyrics.lines.size() == 0)
|
|
||||||
{
|
|
||||||
synchronizedLyrics = null;
|
synchronizedLyrics = null;
|
||||||
lyrics.setVisibility(View.GONE);
|
lyrics.setVisibility(View.GONE);
|
||||||
lyricsLine1.setText(null);
|
lyricsLine1.setText(null);
|
||||||
|
|
@ -219,8 +217,7 @@ public class PlayerAlbumCoverFragment extends AbsMusicServiceFragment implements
|
||||||
String oldLine = lyricsLine2.getText().toString();
|
String oldLine = lyricsLine2.getText().toString();
|
||||||
String line = synchronizedLyrics.getLine(progress);
|
String line = synchronizedLyrics.getLine(progress);
|
||||||
|
|
||||||
if(!oldLine.equals(line) || oldLine.isEmpty())
|
if (!oldLine.equals(line) || oldLine.isEmpty()) {
|
||||||
{
|
|
||||||
lyricsLine1.setText(oldLine);
|
lyricsLine1.setText(oldLine);
|
||||||
lyricsLine2.setText(line);
|
lyricsLine2.setText(line);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue