Merge pull request #737 from Bnyro/master

chapters for fullscreen
This commit is contained in:
Bnyro 2022-07-09 19:26:02 +02:00 committed by GitHub
commit 98801b975a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 92 additions and 5 deletions

View File

@ -27,7 +27,7 @@ class ChaptersAdapter(
chapterTitle.text = chapter.title chapterTitle.text = chapter.title
root.setOnClickListener { root.setOnClickListener {
val chapterStart = chapter.start!!.toLong() * 1000 // s -> ms val chapterStart = chapter.start!! * 1000 // s -> ms
exoPlayer.seekTo(chapterStart) exoPlayer.seekTo(chapterStart)
} }
} }

View File

@ -90,7 +90,9 @@ import kotlinx.coroutines.launch
import org.chromium.net.CronetEngine import org.chromium.net.CronetEngine
import retrofit2.HttpException import retrofit2.HttpException
import java.io.IOException import java.io.IOException
import java.util.*
import java.util.concurrent.Executors import java.util.concurrent.Executors
import kotlin.collections.ArrayList
import kotlin.math.abs import kotlin.math.abs
var isFullScreen = false var isFullScreen = false
@ -137,6 +139,7 @@ class PlayerFragment : Fragment() {
private lateinit var title: String private lateinit var title: String
private lateinit var uploader: String private lateinit var uploader: String
private lateinit var thumbnailUrl: String private lateinit var thumbnailUrl: String
private lateinit var chapters: List<ChapterSegment>
private val sponsorBlockPrefs = SponsorBlockPrefs() private val sponsorBlockPrefs = SponsorBlockPrefs()
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
@ -335,6 +338,7 @@ class PlayerFragment : Fragment() {
binding.linLayout.visibility = View.GONE binding.linLayout.visibility = View.GONE
playerBinding.fullscreen.setImageResource(R.drawable.ic_fullscreen_exit) playerBinding.fullscreen.setImageResource(R.drawable.ic_fullscreen_exit)
playerBinding.exoTitle.visibility = View.VISIBLE playerBinding.exoTitle.visibility = View.VISIBLE
if (chapters.isNotEmpty()) playerBinding.chapterLL.visibility = View.VISIBLE
val mainActivity = activity as MainActivity val mainActivity = activity as MainActivity
val fullscreenOrientationPref = PreferenceHelper val fullscreenOrientationPref = PreferenceHelper
@ -372,6 +376,7 @@ class PlayerFragment : Fragment() {
binding.linLayout.visibility = View.VISIBLE binding.linLayout.visibility = View.VISIBLE
playerBinding.fullscreen.setImageResource(R.drawable.ic_fullscreen) playerBinding.fullscreen.setImageResource(R.drawable.ic_fullscreen)
playerBinding.exoTitle.visibility = View.INVISIBLE playerBinding.exoTitle.visibility = View.INVISIBLE
playerBinding.chapterLL.visibility = View.INVISIBLE
scaleControls(1F) scaleControls(1F)
@ -710,7 +715,10 @@ class PlayerFragment : Fragment() {
enableDoubleTapToSeek() enableDoubleTapToSeek()
// init the chapters recyclerview // 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 // Listener for play and pause icon change
exoPlayer.addListener(object : Player.Listener { exoPlayer.addListener(object : Player.Listener {
@ -936,12 +944,55 @@ class PlayerFragment : Fragment() {
}) })
} }
private fun initializeChapters(chapters: List<ChapterSegment>) { private fun initializeChapters() {
if (chapters.isNotEmpty()) { if (chapters.isNotEmpty()) {
// enable chapters in the video description
binding.chaptersRecView.layoutManager = binding.chaptersRecView.layoutManager =
LinearLayoutManager(this.context, LinearLayoutManager.HORIZONTAL, false) LinearLayoutManager(
context,
LinearLayoutManager.HORIZONTAL,
false
)
binding.chaptersRecView.adapter = ChaptersAdapter(chapters, exoPlayer) binding.chaptersRecView.adapter = ChaptersAdapter(chapters, exoPlayer)
binding.chaptersRecView.visibility = View.VISIBLE binding.chaptersRecView.visibility = View.VISIBLE
// enable chapters in the player
val titles = mutableListOf<String>()
chapters.forEach {
titles += it.title!!
}
playerBinding.chapterLL.setOnClickListener {
MaterialAlertDialogBuilder(requireContext())
.setTitle(R.string.chapters)
.setItems(titles.toTypedArray()) { _, index ->
val position = chapters[index].start!! * 1000
exoPlayer.seekTo(position)
}
.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
} }
} }

View File

@ -6,7 +6,7 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties
data class ChapterSegment( data class ChapterSegment(
var title: String?, var title: String?,
var image: String?, var image: String?,
var start: Int? var start: Long?
) { ) {
constructor() : this("", "", -1) constructor() : this("", "", -1)
} }

View File

@ -0,0 +1,11 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:autoMirrored="true"
android:tint="@android:color/white"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@android:color/white"
android:pathData="M8.59,16.59L13.17,12 8.59,7.41 10,6l6,6 -6,6 -1.41,-1.41z" />
</vector>

View File

@ -136,6 +136,30 @@
android:id="@id/exo_duration" android:id="@id/exo_duration"
style="@style/ExoStyledControls.TimeText.Duration" /> style="@style/ExoStyledControls.TimeText.Duration" />
<LinearLayout
android:id="@+id/chapterLL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:visibility="gone">
<TextView
android:id="@+id/chapter_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="16sp" />
<ImageView
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_gravity="center"
android:layout_marginTop="2dp"
android:layout_marginStart="3dp"
android:src="@drawable/ic_arrow_right" />
</LinearLayout>
</LinearLayout> </LinearLayout>
<LinearLayout <LinearLayout

View File

@ -229,4 +229,5 @@
<string name="twitter">Twitter</string> <string name="twitter">Twitter</string>
<string name="turnInternetOn">Please connect to the internet by turning on WiFi or mobile data.</string> <string name="turnInternetOn">Please connect to the internet by turning on WiFi or mobile data.</string>
<string name="open">Open …</string> <string name="open">Open …</string>
<string name="chapters">Chapters</string>
</resources> </resources>