Added Swipe on Lyrics to change songs

This commit is contained in:
Prathamesh More 2021-12-21 23:01:22 +05:30
parent f660c6147a
commit 9d8eb633af
2 changed files with 34 additions and 7 deletions

View file

@ -136,13 +136,26 @@ class PlayerAlbumCoverFragment : AbsMusicServiceFragment(R.layout.fragment_playe
progressViewUpdateHelper = MusicProgressViewUpdateHelper(this, 500, 1000)
maybeInitLyrics()
lrcView.apply {
setDraggable(true, object : CoverLrcView.OnPlayClickListener {
override fun onPlayClick(time: Long): Boolean {
setDraggable(true) { time ->
MusicPlayerRemote.seekTo(time.toInt())
MusicPlayerRemote.resumePlaying()
return true
true
}
setOnFlingXListener { velocityX ->
when {
velocityX < 0 -> {
MusicPlayerRemote.playNextSong()
true
}
velocityX > 0 -> {
MusicPlayerRemote.playPreviousSong()
true
}
else -> {
false
}
}
}
})
}
}

View file

@ -35,6 +35,7 @@ import android.view.animation.LinearInterpolator
import android.widget.Scroller
import androidx.core.content.ContextCompat
import code.name.monkey.retromusic.R
import code.name.monkey.retromusic.helper.MusicPlayerRemote
import java.io.File
import java.util.*
import kotlin.math.abs
@ -67,6 +68,7 @@ class CoverLrcView @JvmOverloads constructor(
private var mDefaultLabel: String? = null
private var mLrcPadding = 0f
private var mOnPlayClickListener: OnPlayClickListener? = null
private var mOnFlingXListener: OnFlingXListener? = null
private var mAnimator: ValueAnimator? = null
private var mGestureDetector: GestureDetector? = null
private var mScroller: Scroller? = null
@ -131,6 +133,9 @@ class CoverLrcView @JvmOverloads constructor(
velocityX: Float,
velocityY: Float
): Boolean {
if (mOnFlingXListener != null && abs(velocityX) > abs(velocityY)) {
return mOnFlingXListener!!.onFlingX(velocityX)
}
if (hasLrc()) {
mScroller!!.fling(
0,
@ -313,6 +318,10 @@ class CoverLrcView @JvmOverloads constructor(
mOnPlayClickListener = onPlayClickListener
}
fun setOnFlingXListener(onFlingXListener: OnFlingXListener) {
mOnFlingXListener = onFlingXListener
}
/** 设置歌词为空时屏幕中央显示的文字,如“暂无歌词” */
fun setLabel(label: String?) {
runOnUi {
@ -661,6 +670,7 @@ class CoverLrcView @JvmOverloads constructor(
/** 获取歌词距离视图顶部的距离 采用懒加载方式 */
private fun getOffset(line: Int): Float {
if (mLrcEntryList.isEmpty()) return 0F
if (mLrcEntryList[line].offset == Float.MIN_VALUE) {
var offset = (height / 2).toFloat()
for (i in 1..line) {
@ -686,7 +696,7 @@ class CoverLrcView @JvmOverloads constructor(
}
/** 播放按钮点击监听器,点击后应该跳转到指定播放位置 */
interface OnPlayClickListener {
fun interface OnPlayClickListener {
/**
* 播放按钮被点击应该跳转到指定播放位置
*
@ -695,6 +705,10 @@ class CoverLrcView @JvmOverloads constructor(
fun onPlayClick(time: Long): Boolean
}
fun interface OnFlingXListener {
fun onFlingX(velocityX: Float): Boolean
}
companion object {
private const val ADJUST_DURATION: Long = 100
private const val TIMELINE_KEEP_TIME = 4 * DateUtils.SECOND_IN_MILLIS