[Now Playing] Added simple lyrics to full album cover themes

This commit is contained in:
Prathamesh More 2021-12-22 22:25:55 +05:30
parent 1648fc6ef3
commit 9e00965103
8 changed files with 262 additions and 34 deletions

View file

@ -0,0 +1,174 @@
package code.name.monkey.retromusic.fragments.other
import android.content.SharedPreferences
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.FrameLayout
import android.widget.TextView
import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import androidx.preference.PreferenceManager
import code.name.monkey.retromusic.R
import code.name.monkey.retromusic.SHOW_LYRICS
import code.name.monkey.retromusic.databinding.FragmentCoverLyricsBinding
import code.name.monkey.retromusic.fragments.base.AbsMusicServiceFragment
import code.name.monkey.retromusic.fragments.base.AbsPlayerFragment
import code.name.monkey.retromusic.helper.MusicPlayerRemote
import code.name.monkey.retromusic.helper.MusicProgressViewUpdateHelper
import code.name.monkey.retromusic.model.lyrics.AbsSynchronizedLyrics
import code.name.monkey.retromusic.model.lyrics.Lyrics
import code.name.monkey.retromusic.util.LyricUtil
import code.name.monkey.retromusic.util.PreferenceUtil
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.jaudiotagger.audio.exceptions.CannotReadException
import java.io.File
import java.io.FileNotFoundException
class CoverLyricsFragment : AbsMusicServiceFragment(R.layout.fragment_cover_lyrics),
MusicProgressViewUpdateHelper.Callback, SharedPreferences.OnSharedPreferenceChangeListener {
private var progressViewUpdateHelper: MusicProgressViewUpdateHelper? = null
private var _binding: FragmentCoverLyricsBinding? = null
private val binding get() = _binding!!
private val lyricsLayout: FrameLayout get() = binding.playerLyrics
private val lyricsLine1: TextView get() = binding.playerLyricsLine1
private val lyricsLine2: TextView get() = binding.playerLyricsLine2
private var lyrics: Lyrics? = null
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
_binding = FragmentCoverLyricsBinding.bind(view)
progressViewUpdateHelper = MusicProgressViewUpdateHelper(this, 500, 1000)
if (PreferenceUtil.showLyrics) {
progressViewUpdateHelper?.start()
}
}
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
if (key == SHOW_LYRICS) {
if (sharedPreferences?.getBoolean(key, false) == true) {
progressViewUpdateHelper?.start()
binding.root.isVisible = true
updateLyrics()
} else {
progressViewUpdateHelper?.stop()
binding.root.isVisible = false
}
}
}
override fun onPlayingMetaChanged() {
super.onPlayingMetaChanged()
if (PreferenceUtil.showLyrics) {
updateLyrics()
}
}
override fun onServiceConnected() {
super.onServiceConnected()
if (PreferenceUtil.showLyrics) {
updateLyrics()
}
}
private fun updateLyrics() {
lyrics = null
lifecycleScope.launch(Dispatchers.IO) {
val song = MusicPlayerRemote.currentSong
lyrics = try {
val lrcFile: File? = LyricUtil.getSyncedLyricsFile(song)
val data: String = LyricUtil.getStringFromLrc(lrcFile)
Lyrics.parse(song,
if (!TextUtils.isEmpty(data)) {
data
} else {
// Get Embedded Lyrics
LyricUtil.getEmbeddedSyncedLyrics(song.data)
}
)
} catch (err: FileNotFoundException) {
null
} catch (e: CannotReadException) {
null
}
}
}
override fun onUpdateProgressViews(progress: Int, total: Int) {
if (_binding == null) return
if (!isLyricsLayoutVisible()) {
hideLyricsLayout()
return
}
if (lyrics !is AbsSynchronizedLyrics) return
val synchronizedLyrics = lyrics as AbsSynchronizedLyrics
lyricsLayout.visibility = View.VISIBLE
lyricsLayout.alpha = 1f
val oldLine = lyricsLine2.text.toString()
val line = synchronizedLyrics.getLine(progress)
if (oldLine != line || oldLine.isEmpty()) {
lyricsLine1.text = oldLine
lyricsLine2.text = line
lyricsLine1.isVisible = true
lyricsLine2.isVisible = true
lyricsLine2.measure(
View.MeasureSpec.makeMeasureSpec(
lyricsLine2.measuredWidth,
View.MeasureSpec.EXACTLY
),
View.MeasureSpec.UNSPECIFIED
)
val h: Float = lyricsLine2.measuredHeight.toFloat()
lyricsLine1.alpha = 1f
lyricsLine1.translationY = 0f
lyricsLine1.animate().alpha(0f).translationY(-h).duration =
AbsPlayerFragment.VISIBILITY_ANIM_DURATION
lyricsLine2.alpha = 0f
lyricsLine2.translationY = h
lyricsLine2.animate().alpha(1f).translationY(0f).duration =
AbsPlayerFragment.VISIBILITY_ANIM_DURATION
}
}
private fun isLyricsLayoutVisible(): Boolean {
return lyrics != null && lyrics!!.isSynchronized && lyrics!!.isValid
}
private fun hideLyricsLayout() {
lyricsLayout.animate().alpha(0f).setDuration(AbsPlayerFragment.VISIBILITY_ANIM_DURATION)
.withEndAction {
if (_binding == null) return@withEndAction
lyricsLayout.isVisible = false
lyricsLine1.text = null
lyricsLine2.text = null
}
}
override fun onResume() {
super.onResume()
PreferenceManager.getDefaultSharedPreferences(requireContext())
.registerOnSharedPreferenceChangeListener(this)
}
override fun onDestroyView() {
super.onDestroyView()
PreferenceManager.getDefaultSharedPreferences(requireContext())
.unregisterOnSharedPreferenceChangeListener(this)
progressViewUpdateHelper?.stop()
_binding = null
}
}

View file

@ -57,6 +57,14 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/fragment_album_full_card_cover" />
<androidx.fragment.app.FragmentContainerView
android:id="@+id/cover_lyrics"
android:name="code.name.monkey.retromusic.fragments.other.CoverLyricsFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="20dp"
android:layout_gravity="center"/>
</FrameLayout>
<FrameLayout
@ -71,5 +79,6 @@
android:layout_height="match_parent"
tools:layout="@layout/fragment_adaptive_player_playback_controls" />
</FrameLayout>
</LinearLayout>
</FrameLayout>

View file

@ -73,6 +73,14 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:layout="@layout/fragment_album_card_cover" />
<androidx.fragment.app.FragmentContainerView
android:id="@+id/cover_lyrics"
android:name="code.name.monkey.retromusic.fragments.other.CoverLyricsFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="20dp"
android:layout_gravity="center"/>
</FrameLayout>
</LinearLayout>

View file

@ -19,6 +19,13 @@
android:layout_height="match_parent"
tools:layout="@layout/fragment_album_full_cover" />
<androidx.fragment.app.FragmentContainerView
android:id="@+id/cover_lyrics"
android:name="code.name.monkey.retromusic.fragments.other.CoverLyricsFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:elevation="20dp" />
<include layout="@layout/shadow_statusbar_toolbar" />
<FrameLayout

View file

@ -0,0 +1,38 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/playerLyrics"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:alpha="0"
android:clipToPadding="false"
android:elevation="20dp"
android:padding="16dp"
android:visibility="gone"
tools:visibility="visible"
android:layout_gravity="center">
<com.google.android.material.textview.MaterialTextView
android:id="@+id/player_lyrics_line1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center"
android:shadowColor="@color/md_black_1000"
android:shadowRadius="4"
android:textAppearance="@style/TextViewHeadline5"
android:textColor="@color/md_white_1000"
android:visibility="gone" />
<com.google.android.material.textview.MaterialTextView
android:id="@+id/player_lyrics_line2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center"
android:shadowColor="@color/md_black_1000"
android:shadowRadius="4"
android:textAppearance="@style/TextViewHeadline5"
android:textColor="@color/md_white_1000" />
</FrameLayout>

View file

@ -30,6 +30,15 @@
tools:layout="@layout/fragment_album_full_cover" />
<include layout="@layout/shadow_statusbar_toolbar" />
<androidx.fragment.app.FragmentContainerView
android:id="@+id/cover_lyrics"
android:name="code.name.monkey.retromusic.fragments.other.CoverLyricsFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginBottom="20dp"
android:elevation="20dp" />
</FrameLayout>
<FrameLayout
@ -43,6 +52,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/fragment_gradient_controls" />
</FrameLayout>
<FrameLayout

View file

@ -135,42 +135,13 @@
</com.google.android.material.appbar.MaterialToolbar>
<FrameLayout
android:id="@+id/playerLyrics"
<androidx.fragment.app.FragmentContainerView
android:id="@+id/cover_lyrics"
android:name="code.name.monkey.retromusic.fragments.other.CoverLyricsFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:alpha="0"
android:clipToPadding="false"
android:elevation="20dp"
android:padding="16dp"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/playerToolbar"
tools:visibility="visible">
<com.google.android.material.textview.MaterialTextView
android:id="@+id/player_lyrics_line1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center"
android:shadowColor="@color/md_black_1000"
android:shadowRadius="4"
android:textAppearance="@style/TextViewHeadline6"
android:textColor="@color/md_white_1000"
android:visibility="gone" />
<com.google.android.material.textview.MaterialTextView
android:id="@+id/player_lyrics_line2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center"
android:shadowColor="@color/md_black_1000"
android:shadowRadius="4"
android:textAppearance="@style/TextViewHeadline6"
android:textColor="@color/md_white_1000" />
</FrameLayout>
android:elevation="20dp"
app:layout_constraintTop_toBottomOf="@id/playerToolbar"/>
</androidx.constraintlayout.widget.ConstraintLayout>

View file

@ -58,6 +58,17 @@
layout="@layout/status_bar" />
</FrameLayout>
<androidx.fragment.app.FragmentContainerView
android:id="@+id/cover_lyrics"
android:name="code.name.monkey.retromusic.fragments.other.CoverLyricsFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="20dp"
app:layout_constraintBottom_toBottomOf="@+id/playerAlbumCoverFragment"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/statusBarContainer" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout