Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for synchronous lyrics from .LRC files #478

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Changelog is available [here](https://github.com/naman14/Timber/blob/master/Chan

* CyanogenMod's [Eleven Music Player](https://github.com/CyanogenMod/android_packages_apps_Eleven)
* [TimelyTextView](https://github.com/adnan-SM/TimelyTextView)
* [LrcView](https://github.com/wangchenyan/lrcview)
* [MultiViewPager](https://github.com/Pixplicity/MultiViewPager)
* [PlayPauseButton](https://github.com/recruit-lifestyle/PlayPauseButton)
* [CircularSeekBar](https://github.com/devadvance/circularseekbar)
Expand Down
5 changes: 5 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ android {
disable 'MissingTranslation'
disable 'ExtraTranslation'
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

}

Expand Down Expand Up @@ -61,6 +65,7 @@ dependencies {
implementation 'com.squareup.okhttp:okhttp:2.3.0'
implementation 'com.google.code.gson:gson:2.3'
implementation 'de.Maxr1998:track-selector-lib:1.2'
implementation 'me.wcy:lrcview:2.2'

implementation 'com.afollestad.material-dialogs:core:0.9.0.2'
implementation 'com.afollestad.material-dialogs:commons:0.9.0.2'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;
import me.wcy.lrcview.LrcView;

/**
* Created by christoph on 10.12.16.
Expand All @@ -36,35 +37,88 @@ public class LyricsFragment extends Fragment {
private String lyrics = null;
private Toolbar toolbar;
private View rootView;
private String syncLyrics = null;
private LrcView syncLyricsView;
private ActionBar actionBar;
private long audioId = Long.MIN_VALUE;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_lyrics,container,false);

toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
syncLyricsView = (LrcView) rootView.findViewById(R.id.sync_lyrics);

setupToolbar();

loadLyrics();

return rootView;
}

Runnable mUpdateProgress = new Runnable() {

@Override
public void run() {

long position = MusicPlayer.position();
if (LyricsFragment.this.isResumed()) {
long newAudioId = MusicPlayer.getCurrentAudioId();
if (newAudioId != audioId) {
loadLyrics();
}
if (syncLyricsView.getVisibility() == View.VISIBLE) {
syncLyricsView.updateTime(position);
}
syncLyricsView.postDelayed(mUpdateProgress, 50);
}
}
};

private void loadLyrics() {

final View lyricsView = rootView.findViewById(R.id.lyrics);
final TextView poweredbyTextView = (TextView) lyricsView.findViewById(R.id.lyrics_makeitpersonal);
final LrcView syncLyricsView = (LrcView) rootView.findViewById(R.id.sync_lyrics);
poweredbyTextView.setVisibility(View.GONE);
final TextView lyricsTextView = (TextView) lyricsView.findViewById(R.id.lyrics_text);
lyricsTextView.setText(getString(R.string.lyrics_loading));

if (MusicPlayer.getTrackName() != null) {
actionBar.setTitle(MusicPlayer.getTrackName());
} else {
actionBar.setTitle(getString(R.string.app_name));
}
long newAudioId = MusicPlayer.getCurrentAudioId();
if (newAudioId != audioId) {
audioId = newAudioId;
lyrics = null;
syncLyrics = null;
}
String filename = getRealPathFromURI(Uri.parse(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI + "/" + MusicPlayer.getCurrentAudioId()));
if (filename != null && lyrics == null) {
lyrics = LyricsExtractor.getLyrics(new File(filename));
if (filename != null && lyrics == null && syncLyrics == null) {
File mediaFile = new File(filename);
syncLyrics = LyricsExtractor.getSynchronizedLyrics(mediaFile);
if (syncLyrics == null) {
lyrics = LyricsExtractor.getLyrics(mediaFile);
}
}

if (lyrics != null) {
if (syncLyrics != null) {
syncLyricsView.setVisibility(View.VISIBLE);
lyricsView.setVisibility(View.GONE);

syncLyricsView.loadLrc(syncLyrics);
} else if (lyrics != null) {
syncLyricsView.setVisibility(View.GONE);
lyricsView.setVisibility(View.VISIBLE);

lyricsTextView.setText(lyrics);
} else {
syncLyricsView.setVisibility(View.GONE);
lyricsView.setVisibility(View.VISIBLE);

String artist = MusicPlayer.getArtistName();
if (artist != null) {
int i = artist.lastIndexOf(" feat");
Expand Down Expand Up @@ -100,17 +154,15 @@ private void setupToolbar() {

((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);

final ActionBar ab = ((AppCompatActivity) getActivity()).getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(true);
if (MusicPlayer.getTrackName() != null) {
ab.setTitle(MusicPlayer.getTrackName());
}
actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}

@Override
public void onResume() {
super.onResume();
toolbar.setBackgroundColor(Color.TRANSPARENT);
syncLyricsView.post(mUpdateProgress);
}

private String getRealPathFromURI(Uri contentUri) {
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/java/com/naman14/timber/utils/FileUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.naman14.timber.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.stream.Collectors;

public class FileUtils {
public static String readAllText(File file) throws FileNotFoundException {
BufferedReader reader = new BufferedReader(new FileReader(file));
return reader.lines().collect(Collectors.joining(System.lineSeparator()));
}
}
22 changes: 22 additions & 0 deletions app/src/main/java/com/naman14/timber/utils/LyricsExtractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -31,6 +32,20 @@ public static String getLyrics(File file){
return null;
}

public static String getSynchronizedLyrics(File file) {
String directory = file.getParent();
String lrcName = changeOrAppendExtension(file.getName(), ".lrc");
File lrcFile = new File(directory, lrcName);
if (!lrcFile.exists()) {
return null;
}
try {
return FileUtils.readAllText(lrcFile);
} catch (FileNotFoundException e) {
return null;
}
}

private static int readOgg(byte[] buf, InputStream in, int bytesinpage, int skip) throws IOException {
int toread = skip!=-1?skip:buf.length;
int offset = 0;
Expand Down Expand Up @@ -224,4 +239,11 @@ private static int byteArrayToIntLE(byte[] b) {
return b[0] & 0xFF | (b[1] & 0xFF) << 8 | (b[2] & 0xFF) << 16 | (b[3] & 0xFF) << 24;
}

private static String changeOrAppendExtension(String filename, String newExtension) {
if (filename.lastIndexOf('.') != -1) {
filename = filename.substring(0, filename.lastIndexOf('.'));
}
return filename + newExtension;
}

}
7 changes: 7 additions & 0 deletions app/src/main/res/layout/fragment_lyrics.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
android:theme="@style/Theme.AppCompat"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

<me.wcy.lrcview.LrcView
android:id="@+id/sync_lyrics"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:lrcTextGravity="center" />

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lyrics"
android:layout_width="match_parent"
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ allprojects {

ext {
// Sdk and tools
minSdkVersion = 16
minSdkVersion = 26
targetSdkVersion = 28
compileSdkVersion = 28

Expand Down