Merge pull request #518 from tva2000hn/master
Synced .lrc lyrics without putting in the /RetroMusic folder
This commit is contained in:
commit
99ed3d3bbb
4 changed files with 79 additions and 25 deletions
|
@ -7,6 +7,7 @@ import android.os.Build
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.text.InputType
|
import android.text.InputType
|
||||||
import android.text.TextUtils
|
import android.text.TextUtils
|
||||||
|
import android.util.Log
|
||||||
import android.view.*
|
import android.view.*
|
||||||
import androidx.annotation.StringRes
|
import androidx.annotation.StringRes
|
||||||
import androidx.core.content.ContextCompat
|
import androidx.core.content.ContextCompat
|
||||||
|
@ -150,8 +151,13 @@ class LyricsActivity : AbsMusicServiceActivity(), View.OnClickListener, ViewPage
|
||||||
private fun showSyncedLyrics() {
|
private fun showSyncedLyrics() {
|
||||||
var content = ""
|
var content = ""
|
||||||
try {
|
try {
|
||||||
content = LyricUtil.getStringFromFile(song.title, song.artistName)
|
content = LyricUtil.getStringFromFile(song.data, song.artistName)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
|
try{
|
||||||
|
content = LyricUtil.getStringFromFile(song.title, song.artistName)
|
||||||
|
} catch ( e2 : Exception){
|
||||||
|
|
||||||
|
}
|
||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -161,7 +167,7 @@ class LyricsActivity : AbsMusicServiceActivity(), View.OnClickListener, ViewPage
|
||||||
input(hint = getString(R.string.paste_lyrics_here),
|
input(hint = getString(R.string.paste_lyrics_here),
|
||||||
prefill = content,
|
prefill = content,
|
||||||
inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_FLAG_MULTI_LINE) { _, input ->
|
inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_FLAG_MULTI_LINE) { _, input ->
|
||||||
LyricUtil.writeLrcToLoc(song.title, song.artistName, input.toString())
|
LyricUtil.writeLrcToLoc(song.data, song.artistName, input.toString())
|
||||||
}
|
}
|
||||||
positiveButton(android.R.string.ok) {
|
positiveButton(android.R.string.ok) {
|
||||||
updateSong()
|
updateSong()
|
||||||
|
@ -364,10 +370,14 @@ class LyricsActivity : AbsMusicServiceActivity(), View.OnClickListener, ViewPage
|
||||||
|
|
||||||
private fun loadLRCLyrics() {
|
private fun loadLRCLyrics() {
|
||||||
val song = MusicPlayerRemote.currentSong
|
val song = MusicPlayerRemote.currentSong
|
||||||
|
if (LyricUtil.isLrcFile2Exist(song.data, song.artistName)) {
|
||||||
|
showLyricsLocal(LyricUtil.getLocalLyricFile(song.data, song.artistName))
|
||||||
|
} else {
|
||||||
if (LyricUtil.isLrcFileExist(song.title, song.artistName)) {
|
if (LyricUtil.isLrcFileExist(song.title, song.artistName)) {
|
||||||
showLyricsLocal(LyricUtil.getLocalLyricFile(song.title, song.artistName))
|
showLyricsLocal(LyricUtil.getLocalLyricFile(song.title, song.artistName))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun showLyricsLocal(file: File?) {
|
private fun showLyricsLocal(file: File?) {
|
||||||
if (file != null) {
|
if (file != null) {
|
||||||
|
|
|
@ -213,7 +213,7 @@ abstract class AbsPlayerFragment : AbsMusicServiceFragment(),
|
||||||
|
|
||||||
override fun doInBackground(vararg params: Song): Lyrics? {
|
override fun doInBackground(vararg params: Song): Lyrics? {
|
||||||
try {
|
try {
|
||||||
var data: String? = LyricUtil.getStringFromFile(params[0].title, params[0].artistName)
|
var data: String? = LyricUtil.getStringFromFile(params[0].data, params[0].artistName)
|
||||||
return if (TextUtils.isEmpty(data)) {
|
return if (TextUtils.isEmpty(data)) {
|
||||||
data = MusicUtil.getLyrics(params[0])
|
data = MusicUtil.getLyrics(params[0])
|
||||||
return if (TextUtils.isEmpty(data)) {
|
return if (TextUtils.isEmpty(data)) {
|
||||||
|
|
|
@ -42,8 +42,8 @@ public class LrcHelper {
|
||||||
|
|
||||||
private static final String CHARSET = "utf-8";
|
private static final String CHARSET = "utf-8";
|
||||||
//[03:56.00][03:18.00][02:06.00][01:07.00]原谅我这一生不羁放纵爱自由
|
//[03:56.00][03:18.00][02:06.00][01:07.00]原谅我这一生不羁放纵爱自由
|
||||||
private static final String LINE_REGEX = "((\\[\\d{2}:\\d{2}\\.\\d{2}])+)(.*)";
|
private static final String LINE_REGEX = "((\\[\\d{2}:\\d{2}\\.\\d{2,3}])+)(.*)";
|
||||||
private static final String TIME_REGEX = "\\[(\\d{2}):(\\d{2})\\.(\\d{2})]";
|
private static final String TIME_REGEX = "\\[(\\d{2}):(\\d{2})\\.(\\d{2,3})]";
|
||||||
|
|
||||||
public static List<Lrc> parseLrcFromAssets(Context context, String fileName) {
|
public static List<Lrc> parseLrcFromAssets(Context context, String fileName) {
|
||||||
try {
|
try {
|
||||||
|
@ -127,8 +127,14 @@ public class LrcHelper {
|
||||||
String mil = timeMatcher.group(3);
|
String mil = timeMatcher.group(3);
|
||||||
Lrc lrc = new Lrc();
|
Lrc lrc = new Lrc();
|
||||||
if (content != null && content.length() != 0) {
|
if (content != null && content.length() != 0) {
|
||||||
|
if(Integer.parseInt(mil)< 100){
|
||||||
lrc.setTime(Long.parseLong(min) * 60 * 1000 + Long.parseLong(sec) * 1000
|
lrc.setTime(Long.parseLong(min) * 60 * 1000 + Long.parseLong(sec) * 1000
|
||||||
+ Long.parseLong(mil) * 10);
|
+ Long.parseLong(mil) * 10);}
|
||||||
|
else{
|
||||||
|
lrc.setTime(Long.parseLong(min) * 60 * 1000 + Long.parseLong(sec) * 1000
|
||||||
|
+ Long.parseLong(mil)
|
||||||
|
);
|
||||||
|
}
|
||||||
lrc.setText(content);
|
lrc.setText(content);
|
||||||
lrcs.add(lrc);
|
lrcs.add(lrc);
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
package code.name.monkey.retromusic.util;
|
package code.name.monkey.retromusic.util;
|
||||||
|
|
||||||
import android.util.Base64;
|
import android.util.Base64;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
@ -40,11 +41,11 @@ public class LyricUtil {
|
||||||
public static File writeLrcToLoc(@NonNull String title, @NonNull String artist, @NonNull String lrcContext) {
|
public static File writeLrcToLoc(@NonNull String title, @NonNull String artist, @NonNull String lrcContext) {
|
||||||
FileWriter writer = null;
|
FileWriter writer = null;
|
||||||
try {
|
try {
|
||||||
File file = new File(getLrcPath(title, artist));
|
File file = new File(getLrcPath2(title, artist));
|
||||||
if (!file.getParentFile().exists()) {
|
if (!file.exists()) {
|
||||||
file.getParentFile().mkdirs();
|
file.mkdirs();
|
||||||
}
|
}
|
||||||
writer = new FileWriter(getLrcPath(title, artist));
|
writer = new FileWriter(getLrcPath2(title, artist));
|
||||||
writer.write(lrcContext);
|
writer.write(lrcContext);
|
||||||
return file;
|
return file;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
@ -70,17 +71,46 @@ public class LyricUtil {
|
||||||
return file.exists();
|
return file.exists();
|
||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
public static boolean isLrcFile2Exist(@NonNull String title, @NonNull String artist) {
|
||||||
public static File getLocalLyricFile(@NonNull String title, @NonNull String artist) {
|
File file = new File(getLrcPath2(title, artist));
|
||||||
File file = new File(getLrcPath(title, artist));
|
|
||||||
if (file.exists()) {
|
return file.exists();
|
||||||
return file;
|
|
||||||
} else {
|
|
||||||
return new File("lyric file not exist");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String getLrcPath(String title, String artist) {
|
@NonNull
|
||||||
|
public static File getLocalLyricFile(@NonNull String title, @NonNull String artist) {
|
||||||
|
try{
|
||||||
|
File file = new File(getLrcPath(title, artist));
|
||||||
|
File file2 = new File(getLrcPath2(title, artist));
|
||||||
|
if (file.exists()) {
|
||||||
|
|
||||||
|
return file;
|
||||||
|
} else if (file2.exists()) {
|
||||||
|
|
||||||
|
return file2;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
|
||||||
|
return new File("lyric file not exist");
|
||||||
|
}} catch (Exception dfs){
|
||||||
|
dfs.printStackTrace();
|
||||||
|
return new File("lyric file not exist");
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static String getLrcPath2(String title, String artist) {
|
||||||
|
String x2;
|
||||||
|
if(title.endsWith(".flac")||title.endsWith(".mogg")||title.endsWith(".alac")||title.endsWith(".aiff")||title.endsWith(".webv")){
|
||||||
|
x2= title.substring(0, title.length() -5 ) + ".lrc";
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
x2= title.substring(0, title.length() -4 ) + ".lrc";}
|
||||||
|
|
||||||
|
|
||||||
|
return x2;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getLrcPath(String title, String artist) {
|
||||||
return lrcRootPath + title + " - " + artist + ".lrc";
|
return lrcRootPath + title + " - " + artist + ".lrc";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -103,10 +133,18 @@ public class LyricUtil {
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
public static String getStringFromFile(@NonNull String title, @NonNull String artist) throws Exception {
|
public static String getStringFromFile(@NonNull String title, @NonNull String artist) throws Exception {
|
||||||
File file = new File(getLrcPath(title, artist));
|
File file;
|
||||||
|
File file2 = new File(getLrcPath(title, artist));
|
||||||
|
File file3 = new File(getLrcPath2(title, artist));
|
||||||
|
if(file2.exists()){
|
||||||
|
file = file2;
|
||||||
|
} else {
|
||||||
|
file = file3;
|
||||||
|
}
|
||||||
FileInputStream fin = new FileInputStream(file);
|
FileInputStream fin = new FileInputStream(file);
|
||||||
String ret = convertStreamToString(fin);
|
String ret = convertStreamToString(fin);
|
||||||
fin.close();
|
fin.close();
|
||||||
|
// Log.d("damn2",ret);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue