From 83899affaad2e6673333538eac7bc0c8ab80c430 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Mon, 19 Jun 2023 12:56:53 +0200 Subject: [PATCH] Chapters support in audio player --- .../github/libretube/helpers/PlayerHelper.kt | 20 ++++++++++++++++++- .../libretube/services/OnlinePlayerService.kt | 3 ++- .../ui/fragments/AudioPlayerFragment.kt | 20 +++++++++++++++---- .../libretube/ui/fragments/PlayerFragment.kt | 10 +--------- .../main/res/layout/fragment_audio_player.xml | 4 ++-- 5 files changed, 40 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt b/app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt index a7906b1cb..565122fe0 100644 --- a/app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt @@ -6,6 +6,7 @@ import android.content.Context import android.content.Intent import android.content.pm.ActivityInfo import android.net.Uri +import android.text.format.DateUtils import android.util.Base64 import android.view.accessibility.CaptioningManager import android.widget.Toast @@ -23,11 +24,13 @@ import androidx.media3.exoplayer.ExoPlayer import androidx.media3.exoplayer.LoadControl import androidx.media3.ui.CaptionStyleCompat import com.github.libretube.R +import com.github.libretube.api.obj.ChapterSegment import com.github.libretube.api.obj.Segment import com.github.libretube.api.obj.Streams import com.github.libretube.constants.PreferenceKeys import com.github.libretube.enums.PlayerEvent import com.github.libretube.enums.SbSkipOptions +import com.google.android.material.dialog.MaterialAlertDialogBuilder import kotlin.math.absoluteValue import kotlin.math.roundToInt @@ -440,7 +443,7 @@ object PlayerHelper { if ((duration - currentPosition).absoluteValue < 500) continue if (currentPosition in segmentStart until segmentEnd) { - if (sponsorBlockConfig.get(segment.category) == SbSkipOptions.AUTOMATIC) { + if (sponsorBlockConfig[segment.category] == SbSkipOptions.AUTOMATIC) { if (sponsorBlockNotifications) { runCatching { Toast.makeText(context, R.string.segment_skipped, Toast.LENGTH_SHORT) @@ -455,4 +458,19 @@ object PlayerHelper { } return null } + + /** + * Show a dialog with the chapters provided, even if the list is empty + */ + fun showChaptersDialog(context: Context, chapters: List, player: ExoPlayer) { + val titles = chapters.map { chapter -> + "(${DateUtils.formatElapsedTime(chapter.start)}) ${chapter.title}" + } + MaterialAlertDialogBuilder(context) + .setTitle(R.string.chapters) + .setItems(titles.toTypedArray()) { _, index -> + player.seekTo(chapters[index].start * 1000) + } + .show() + } } diff --git a/app/src/main/java/com/github/libretube/services/OnlinePlayerService.kt b/app/src/main/java/com/github/libretube/services/OnlinePlayerService.kt index c964a933f..6a865a523 100644 --- a/app/src/main/java/com/github/libretube/services/OnlinePlayerService.kt +++ b/app/src/main/java/com/github/libretube/services/OnlinePlayerService.kt @@ -65,7 +65,8 @@ class OnlinePlayerService : LifecycleService() { /** * The response that gets when called the Api. */ - private var streams: Streams? = null + var streams: Streams? = null + private set /** * The [ExoPlayer] player. Followed tutorial [here](https://developer.android.com/codelabs/exoplayer-intro) diff --git a/app/src/main/java/com/github/libretube/ui/fragments/AudioPlayerFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/AudioPlayerFragment.kt index 7d884372e..31dfc75d3 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/AudioPlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/AudioPlayerFragment.kt @@ -13,6 +13,7 @@ import android.text.format.DateUtils import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.widget.Toast import androidx.constraintlayout.motion.widget.MotionLayout import androidx.constraintlayout.motion.widget.TransitionAdapter import androidx.fragment.app.Fragment @@ -29,6 +30,7 @@ import com.github.libretube.helpers.AudioHelper import com.github.libretube.helpers.BackgroundHelper import com.github.libretube.helpers.ImageHelper import com.github.libretube.helpers.NavigationHelper +import com.github.libretube.helpers.PlayerHelper import com.github.libretube.obj.ShareData import com.github.libretube.services.OnlinePlayerService import com.github.libretube.ui.activities.MainActivity @@ -153,10 +155,20 @@ class AudioPlayerFragment : Fragment(), AudioPlayerOptions { ).show(childFragmentManager, null) } - binding.close.setOnClickListener { - activity?.unbindService(connection) - BackgroundHelper.stopBackgroundPlay(requireContext()) - killFragment() + binding.chapters.setOnClickListener { + val playerService = playerService ?: return@setOnClickListener + if (playerService.streams == null || playerService.player == null) return@setOnClickListener + + if (playerService.streams!!.chapters.isEmpty()) { + Toast.makeText(context, R.string.emptyList, Toast.LENGTH_SHORT).show() + return@setOnClickListener + } + + PlayerHelper.showChaptersDialog( + requireContext(), + playerService.streams!!.chapters, + playerService.player!! + ) } binding.miniPlayerClose.setOnClickListener { diff --git a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt index 6097176a8..ba007ee8f 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt @@ -1191,16 +1191,8 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { binding.chaptersRecView.adapter = ChaptersAdapter(chapters, exoPlayer) // enable the chapters dialog in the player - val titles = chapters.map { chapter -> - "(${DateUtils.formatElapsedTime(chapter.start)}) ${chapter.title}" - } playerBinding.chapterLL.setOnClickListener { - MaterialAlertDialogBuilder(requireContext()) - .setTitle(R.string.chapters) - .setItems(titles.toTypedArray()) { _, index -> - exoPlayer.seekTo(chapters[index].start * 1000) - } - .show() + PlayerHelper.showChaptersDialog(requireContext(), chapters, exoPlayer) } setCurrentChapterName() diff --git a/app/src/main/res/layout/fragment_audio_player.xml b/app/src/main/res/layout/fragment_audio_player.xml index c6acee271..c409533f4 100644 --- a/app/src/main/res/layout/fragment_audio_player.xml +++ b/app/src/main/res/layout/fragment_audio_player.xml @@ -238,9 +238,9 @@ android:src="@drawable/ic_share" /> + android:src="@drawable/ic_frame" />