From b38cb661d0cc6544b89ab9b36f9962339c909791 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sat, 9 Jul 2022 18:51:09 +0200 Subject: [PATCH 1/3] add chapters for full screen mode --- .../libretube/adapters/ChaptersAdapter.kt | 2 +- .../libretube/fragments/PlayerFragment.kt | 25 ++++++++++++++++++- .../github/libretube/obj/ChapterSegment.kt | 2 +- .../layout/exo_styled_player_control_view.xml | 15 +++++++++++ app/src/main/res/values/strings.xml | 1 + 5 files changed, 42 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/github/libretube/adapters/ChaptersAdapter.kt b/app/src/main/java/com/github/libretube/adapters/ChaptersAdapter.kt index 22b055a1e..6a28be080 100644 --- a/app/src/main/java/com/github/libretube/adapters/ChaptersAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/ChaptersAdapter.kt @@ -27,7 +27,7 @@ class ChaptersAdapter( chapterTitle.text = chapter.title root.setOnClickListener { - val chapterStart = chapter.start!!.toLong() * 1000 // s -> ms + val chapterStart = chapter.start!! * 1000 // s -> ms exoPlayer.seekTo(chapterStart) } } diff --git a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt index c1b2665bc..dfc90e585 100644 --- a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt @@ -335,6 +335,7 @@ class PlayerFragment : Fragment() { binding.linLayout.visibility = View.GONE playerBinding.fullscreen.setImageResource(R.drawable.ic_fullscreen_exit) playerBinding.exoTitle.visibility = View.VISIBLE + playerBinding.chapterName.visibility = View.VISIBLE val mainActivity = activity as MainActivity val fullscreenOrientationPref = PreferenceHelper @@ -372,6 +373,7 @@ class PlayerFragment : Fragment() { binding.linLayout.visibility = View.VISIBLE playerBinding.fullscreen.setImageResource(R.drawable.ic_fullscreen) playerBinding.exoTitle.visibility = View.INVISIBLE + playerBinding.chapterName.visibility = View.INVISIBLE scaleControls(1F) @@ -938,10 +940,31 @@ class PlayerFragment : Fragment() { private fun initializeChapters(chapters: List) { if (chapters.isNotEmpty()) { + // enable chapters in the video description binding.chaptersRecView.layoutManager = - LinearLayoutManager(this.context, LinearLayoutManager.HORIZONTAL, false) + LinearLayoutManager( + context, + LinearLayoutManager.HORIZONTAL, + false + ) binding.chaptersRecView.adapter = ChaptersAdapter(chapters, exoPlayer) binding.chaptersRecView.visibility = View.VISIBLE + + // enable chapters in the player + val titles = mutableListOf() + chapters.forEach { + titles += it.title!! + } + playerBinding.chapterName.text = chapters[0].title + playerBinding.chapterName.setOnClickListener{ + MaterialAlertDialogBuilder(requireContext()) + .setTitle(R.string.chapters) + .setItems(titles.toTypedArray()) { _, index -> + val position = chapters[index].start!! * 1000 + exoPlayer.seekTo(position) + } + .show() + } } } diff --git a/app/src/main/java/com/github/libretube/obj/ChapterSegment.kt b/app/src/main/java/com/github/libretube/obj/ChapterSegment.kt index 517b6d342..eb47218b3 100644 --- a/app/src/main/java/com/github/libretube/obj/ChapterSegment.kt +++ b/app/src/main/java/com/github/libretube/obj/ChapterSegment.kt @@ -6,7 +6,7 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties data class ChapterSegment( var title: String?, var image: String?, - var start: Int? + var start: Long? ) { constructor() : this("", "", -1) } diff --git a/app/src/main/res/layout/exo_styled_player_control_view.xml b/app/src/main/res/layout/exo_styled_player_control_view.xml index d50a3cebf..c5f9540f5 100644 --- a/app/src/main/res/layout/exo_styled_player_control_view.xml +++ b/app/src/main/res/layout/exo_styled_player_control_view.xml @@ -136,6 +136,21 @@ android:id="@id/exo_duration" style="@style/ExoStyledControls.TimeText.Duration" /> + + + + + + Twitter Please connect to the internet by turning on WiFi or mobile data. Open … + Chapters \ No newline at end of file From 1202049d7b8a9771dab531c78cd720d327f567cc Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sat, 9 Jul 2022 19:25:06 +0200 Subject: [PATCH 2/3] dynamic chapter name --- .../libretube/fragments/PlayerFragment.kt | 39 ++++++++++++++++--- app/src/main/res/drawable/ic_arrow_right.xml | 11 ++++++ .../layout/exo_styled_player_control_view.xml | 19 ++++++--- 3 files changed, 58 insertions(+), 11 deletions(-) create mode 100644 app/src/main/res/drawable/ic_arrow_right.xml diff --git a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt index dfc90e585..e4e8a6645 100644 --- a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt @@ -90,7 +90,9 @@ import kotlinx.coroutines.launch import org.chromium.net.CronetEngine import retrofit2.HttpException import java.io.IOException +import java.util.* import java.util.concurrent.Executors +import kotlin.collections.ArrayList import kotlin.math.abs var isFullScreen = false @@ -137,6 +139,7 @@ class PlayerFragment : Fragment() { private lateinit var title: String private lateinit var uploader: String private lateinit var thumbnailUrl: String + private lateinit var chapters: List private val sponsorBlockPrefs = SponsorBlockPrefs() override fun onCreate(savedInstanceState: Bundle?) { @@ -335,7 +338,7 @@ class PlayerFragment : Fragment() { binding.linLayout.visibility = View.GONE playerBinding.fullscreen.setImageResource(R.drawable.ic_fullscreen_exit) playerBinding.exoTitle.visibility = View.VISIBLE - playerBinding.chapterName.visibility = View.VISIBLE + if (chapters.isNotEmpty()) playerBinding.chapterLL.visibility = View.VISIBLE val mainActivity = activity as MainActivity val fullscreenOrientationPref = PreferenceHelper @@ -373,7 +376,7 @@ class PlayerFragment : Fragment() { binding.linLayout.visibility = View.VISIBLE playerBinding.fullscreen.setImageResource(R.drawable.ic_fullscreen) playerBinding.exoTitle.visibility = View.INVISIBLE - playerBinding.chapterName.visibility = View.INVISIBLE + playerBinding.chapterLL.visibility = View.INVISIBLE scaleControls(1F) @@ -712,7 +715,10 @@ class PlayerFragment : Fragment() { enableDoubleTapToSeek() // init the chapters recyclerview - if (response.chapters != null) initializeChapters(response.chapters) + if (response.chapters != null) { + chapters = response.chapters + initializeChapters() + } // Listener for play and pause icon change exoPlayer.addListener(object : Player.Listener { @@ -938,7 +944,7 @@ class PlayerFragment : Fragment() { }) } - private fun initializeChapters(chapters: List) { + private fun initializeChapters() { if (chapters.isNotEmpty()) { // enable chapters in the video description binding.chaptersRecView.layoutManager = @@ -955,8 +961,7 @@ class PlayerFragment : Fragment() { chapters.forEach { titles += it.title!! } - playerBinding.chapterName.text = chapters[0].title - playerBinding.chapterName.setOnClickListener{ + playerBinding.chapterLL.setOnClickListener { MaterialAlertDialogBuilder(requireContext()) .setTitle(R.string.chapters) .setItems(titles.toTypedArray()) { _, index -> @@ -965,9 +970,31 @@ class PlayerFragment : Fragment() { } .show() } + setCurrentChapterName() } } + // set the name of the video chapter in the exoPlayerView + private fun setCurrentChapterName() { + // call the function again in 100ms + exoPlayerView.postDelayed(this::setCurrentChapterName, 100) + + val currentPosition = exoPlayer.currentPosition + var chapterName: String? = null + val reversedChapters = chapters.toMutableList() + + // reverse the chapters to start at the end + reversedChapters.reverse() + reversedChapters.forEach { + // check whether the chapter start is greater than the current player position + if (it.start!! * 1000 >= currentPosition) chapterName = it.title + } + Log.e(TAG, chapterName.toString()) + // change the chapter name textView text to the chapterName + if (chapterName != null && chapterName != playerBinding.chapterName.text) + playerBinding.chapterName.text = chapterName + } + private fun setMediaSource( subtitle: MutableList, videoUri: Uri, diff --git a/app/src/main/res/drawable/ic_arrow_right.xml b/app/src/main/res/drawable/ic_arrow_right.xml new file mode 100644 index 000000000..fdda75ce4 --- /dev/null +++ b/app/src/main/res/drawable/ic_arrow_right.xml @@ -0,0 +1,11 @@ + + + diff --git a/app/src/main/res/layout/exo_styled_player_control_view.xml b/app/src/main/res/layout/exo_styled_player_control_view.xml index c5f9540f5..1473dad8d 100644 --- a/app/src/main/res/layout/exo_styled_player_control_view.xml +++ b/app/src/main/res/layout/exo_styled_player_control_view.xml @@ -137,17 +137,26 @@ style="@style/ExoStyledControls.TimeText.Duration" /> + android:layout_height="wrap_content" + android:layout_marginStart="20dp" + android:visibility="gone"> + android:layout_gravity="center" + android:textSize="16sp" /> + + From 3a18967521afdda2adb88536833bc4259653116d Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sat, 9 Jul 2022 19:25:55 +0200 Subject: [PATCH 3/3] cleanup --- .../main/java/com/github/libretube/fragments/PlayerFragment.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt index e4e8a6645..8ff421e77 100644 --- a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt @@ -991,8 +991,9 @@ class PlayerFragment : Fragment() { } Log.e(TAG, chapterName.toString()) // change the chapter name textView text to the chapterName - if (chapterName != null && chapterName != playerBinding.chapterName.text) + if (chapterName != null && chapterName != playerBinding.chapterName.text) { playerBinding.chapterName.text = chapterName + } } private fun setMediaSource(