feat: forward and rewind controls for audio player

This commit is contained in:
Bnyro 2023-08-04 18:28:58 +02:00
parent b428b9073a
commit d3d78a93ce
6 changed files with 83 additions and 10 deletions

View File

@ -0,0 +1,10 @@
package com.github.libretube.extensions
import androidx.media3.common.Player
/**
* Forward or rewind by the provided [timeDiff] in milliseconds
*/
fun Player.seekBy(timeDiff: Long) {
seekTo(currentPosition + timeDiff)
}

View File

@ -26,6 +26,7 @@ import com.github.libretube.constants.IntentData
import com.github.libretube.databinding.FragmentAudioPlayerBinding import com.github.libretube.databinding.FragmentAudioPlayerBinding
import com.github.libretube.enums.ShareObjectType import com.github.libretube.enums.ShareObjectType
import com.github.libretube.extensions.normalize import com.github.libretube.extensions.normalize
import com.github.libretube.extensions.seekBy
import com.github.libretube.extensions.toID import com.github.libretube.extensions.toID
import com.github.libretube.helpers.AudioHelper import com.github.libretube.helpers.AudioHelper
import com.github.libretube.helpers.BackgroundHelper import com.github.libretube.helpers.BackgroundHelper
@ -129,6 +130,16 @@ class AudioPlayerFragment : Fragment(), AudioPlayerOptions {
PlayingQueue.onQueueItemSelected(currentIndex + 1) PlayingQueue.onQueueItemSelected(currentIndex + 1)
} }
listOf(binding.forwardTV, binding.rewindTV).forEach {
it.text = (PlayerHelper.seekIncrement / 1000).toString()
}
binding.rewindFL.setOnClickListener {
playerService?.player?.seekBy(-PlayerHelper.seekIncrement)
}
binding.forwardFL.setOnClickListener {
playerService?.player?.seekBy(PlayerHelper.seekIncrement)
}
binding.openQueue.setOnClickListener { binding.openQueue.setOnClickListener {
PlayingQueueSheet().show(childFragmentManager) PlayingQueueSheet().show(childFragmentManager)
} }

View File

@ -73,6 +73,7 @@ import com.github.libretube.enums.ShareObjectType
import com.github.libretube.extensions.formatShort import com.github.libretube.extensions.formatShort
import com.github.libretube.extensions.hideKeyboard import com.github.libretube.extensions.hideKeyboard
import com.github.libretube.extensions.parcelable import com.github.libretube.extensions.parcelable
import com.github.libretube.extensions.seekBy
import com.github.libretube.extensions.setMetadata import com.github.libretube.extensions.setMetadata
import com.github.libretube.extensions.toID import com.github.libretube.extensions.toID
import com.github.libretube.extensions.toastFromMainDispatcher import com.github.libretube.extensions.toastFromMainDispatcher
@ -214,11 +215,11 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
} }
PlayerEvent.Forward -> { PlayerEvent.Forward -> {
exoPlayer.seekTo(exoPlayer.currentPosition + PlayerHelper.seekIncrement) exoPlayer.seekBy(PlayerHelper.seekIncrement)
} }
PlayerEvent.Rewind -> { PlayerEvent.Rewind -> {
exoPlayer.seekTo(exoPlayer.currentPosition - PlayerHelper.seekIncrement) exoPlayer.seekBy(-PlayerHelper.seekIncrement)
} }
PlayerEvent.Next -> { PlayerEvent.Next -> {

View File

@ -40,6 +40,7 @@ import com.github.libretube.databinding.PlayerGestureControlsViewBinding
import com.github.libretube.extensions.dpToPx import com.github.libretube.extensions.dpToPx
import com.github.libretube.extensions.normalize import com.github.libretube.extensions.normalize
import com.github.libretube.extensions.round import com.github.libretube.extensions.round
import com.github.libretube.extensions.seekBy
import com.github.libretube.helpers.AudioHelper import com.github.libretube.helpers.AudioHelper
import com.github.libretube.helpers.BrightnessHelper import com.github.libretube.helpers.BrightnessHelper
import com.github.libretube.helpers.PlayerHelper import com.github.libretube.helpers.PlayerHelper
@ -267,10 +268,10 @@ open class CustomExoPlayerView(
it?.text = seekIncrementText it?.text = seekIncrementText
} }
binding.forwardBTN.setOnClickListener { binding.forwardBTN.setOnClickListener {
player?.seekTo(player!!.currentPosition + PlayerHelper.seekIncrement) player?.seekBy(PlayerHelper.seekIncrement)
} }
binding.rewindBTN.setOnClickListener { binding.rewindBTN.setOnClickListener {
player?.seekTo(player!!.currentPosition - PlayerHelper.seekIncrement) player?.seekBy(-PlayerHelper.seekIncrement)
} }
if (PlayerHelper.doubleTapToSeek) return if (PlayerHelper.doubleTapToSeek) return
@ -363,7 +364,7 @@ open class CustomExoPlayerView(
} }
private fun rewind() { private fun rewind() {
player?.seekTo((player?.currentPosition ?: 0L) - PlayerHelper.seekIncrement) player?.seekBy(-PlayerHelper.seekIncrement)
// show the rewind button // show the rewind button
doubleTapOverlayBinding?.apply { doubleTapOverlayBinding?.apply {
@ -378,7 +379,7 @@ open class CustomExoPlayerView(
} }
private fun forward() { private fun forward() {
player?.seekTo(player!!.currentPosition + PlayerHelper.seekIncrement) player?.seekBy(PlayerHelper.seekIncrement)
// show the forward button // show the forward button
doubleTapOverlayBinding?.apply { doubleTapOverlayBinding?.apply {

View File

@ -30,6 +30,7 @@ import com.github.libretube.R
import com.github.libretube.constants.BACKGROUND_CHANNEL_ID import com.github.libretube.constants.BACKGROUND_CHANNEL_ID
import com.github.libretube.constants.IntentData import com.github.libretube.constants.IntentData
import com.github.libretube.constants.PLAYER_NOTIFICATION_ID import com.github.libretube.constants.PLAYER_NOTIFICATION_ID
import com.github.libretube.extensions.seekBy
import com.github.libretube.helpers.BackgroundHelper import com.github.libretube.helpers.BackgroundHelper
import com.github.libretube.helpers.ImageHelper import com.github.libretube.helpers.ImageHelper
import com.github.libretube.helpers.PlayerHelper import com.github.libretube.helpers.PlayerHelper
@ -251,11 +252,11 @@ class NowPlayingNotification(
} }
REWIND -> { REWIND -> {
player.seekTo(player.currentPosition - PlayerHelper.seekIncrement) player.seekBy(-PlayerHelper.seekIncrement)
} }
FORWARD -> { FORWARD -> {
player.seekTo(player.currentPosition + PlayerHelper.seekIncrement) player.seekBy(PlayerHelper.seekIncrement)
} }
PLAY_PAUSE -> { PLAY_PAUSE -> {

View File

@ -186,6 +186,31 @@
android:layout_marginTop="10dp" android:layout_marginTop="10dp"
android:layout_marginBottom="36dp"> android:layout_marginBottom="36dp">
<FrameLayout
android:id="@+id/rewindFL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="?attr/selectableItemBackgroundBorderless">
<ImageView
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_gravity="center"
android:src="@drawable/ic_rewind_md" />
<TextView
android:id="@+id/rewindTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="2dp"
android:textSize="8sp"
android:textStyle="bold"
tools:ignore="SmallSp" />
</FrameLayout>
<ImageView <ImageView
android:id="@+id/prev" android:id="@+id/prev"
android:layout_width="48dp" android:layout_width="48dp"
@ -213,9 +238,33 @@
android:layout_height="48dp" android:layout_height="48dp"
android:layout_gravity="center" android:layout_gravity="center"
android:layout_marginHorizontal="20dp" android:layout_marginHorizontal="20dp"
android:background="?attr/selectableItemBackgroundBorderless"
android:src="@drawable/ic_next" /> android:src="@drawable/ic_next" />
<FrameLayout
android:id="@+id/forwardFL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="?attr/selectableItemBackgroundBorderless">
<ImageView
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_gravity="center"
android:src="@drawable/ic_forward_md" />
<TextView
android:id="@+id/forwardTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="2dp"
android:textSize="8sp"
android:textStyle="bold"
tools:ignore="SmallSp" />
</FrameLayout>
</LinearLayout> </LinearLayout>
<com.google.android.material.card.MaterialCardView <com.google.android.material.card.MaterialCardView
@ -258,7 +307,7 @@
android:id="@+id/chapters" android:id="@+id/chapters"
style="@style/AudioPlayerButton" style="@style/AudioPlayerButton"
android:src="@drawable/ic_frame" android:src="@drawable/ic_frame"
android:visibility="gone"/> android:visibility="gone" />
</LinearLayout> </LinearLayout>