custom subtitles dialog

This commit is contained in:
Bnyro 2022-07-16 22:49:32 +02:00
parent 36246255c4
commit b9a6d5aa88
8 changed files with 87 additions and 21 deletions

View File

@ -77,6 +77,7 @@ import com.google.android.exoplayer2.source.DefaultMediaSourceFactory
import com.google.android.exoplayer2.source.MediaSource import com.google.android.exoplayer2.source.MediaSource
import com.google.android.exoplayer2.source.MergingMediaSource import com.google.android.exoplayer2.source.MergingMediaSource
import com.google.android.exoplayer2.source.ProgressiveMediaSource import com.google.android.exoplayer2.source.ProgressiveMediaSource
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector
import com.google.android.exoplayer2.ui.AspectRatioFrameLayout import com.google.android.exoplayer2.ui.AspectRatioFrameLayout
import com.google.android.exoplayer2.ui.CaptionStyleCompat import com.google.android.exoplayer2.ui.CaptionStyleCompat
import com.google.android.exoplayer2.ui.PlayerNotificationManager import com.google.android.exoplayer2.ui.PlayerNotificationManager
@ -122,6 +123,7 @@ class PlayerFragment : Fragment() {
private var isLoading = true private var isLoading = true
private lateinit var exoPlayerView: StyledPlayerView private lateinit var exoPlayerView: StyledPlayerView
private lateinit var exoPlayer: ExoPlayer private lateinit var exoPlayer: ExoPlayer
private lateinit var trackSelector: DefaultTrackSelector
private lateinit var segmentData: Segments private lateinit var segmentData: Segments
private var relatedStreamsEnabled = true private var relatedStreamsEnabled = true
@ -1165,7 +1167,9 @@ class PlayerFragment : Fragment() {
} }
} }
// create a list of subtitles // create a list of subtitles
subtitle = mutableListOf<SubtitleConfiguration>() subtitle = mutableListOf()
val subtitlesNamesList = mutableListOf(context?.getString(R.string.none)!!)
val subtitleCodesList = mutableListOf("")
response.subtitles!!.forEach { response.subtitles!!.forEach {
subtitle.add( subtitle.add(
SubtitleConfiguration.Builder(it.url!!.toUri()) SubtitleConfiguration.Builder(it.url!!.toUri())
@ -1173,7 +1177,51 @@ class PlayerFragment : Fragment() {
.setLanguage(it.code) // The subtitle language (optional). .setLanguage(it.code) // The subtitle language (optional).
.build() .build()
) )
subtitlesNamesList += it.name!!
subtitleCodesList += it.code!!
} }
// captions selection dialog
// hide caption selection view if no subtitles available
if (response.subtitles.isEmpty()) playerBinding.captions.visibility = View.GONE
playerBinding.captions.setOnClickListener {
MaterialAlertDialogBuilder(requireContext())
.setTitle(R.string.captions)
.setItems(subtitlesNamesList.toTypedArray()) { _, index ->
val newParams = if (index != 0) {
// caption selected
// get the caption name and language
val captionLanguage = subtitlesNamesList[index]
val captionLanguageCode = subtitleCodesList[index]
// update the icon
playerBinding.captions.setImageResource(R.drawable.ic_caption)
playerBinding.captions.setColorFilter(Color.WHITE)
// select the new caption preference
trackSelector.buildUponParameters()
.setPreferredTextLanguages(
captionLanguage,
captionLanguageCode
)
.setPreferredTextRoleFlags(C.ROLE_FLAG_CAPTION)
} else {
// none selected
playerBinding.captions.setImageResource(R.drawable.ic_caption_outlined)
playerBinding.captions.setColorFilter(Color.GRAY)
// disable captions
trackSelector.buildUponParameters()
.setPreferredTextLanguage("")
}
// set the new caption language
trackSelector.setParameters(newParams)
}
.show()
}
// set media source and resolution in the beginning // set media source and resolution in the beginning
setStreamSource( setStreamSource(
response, response,
@ -1222,11 +1270,11 @@ class PlayerFragment : Fragment() {
) { ) {
val defRes = PreferenceHelper.getString( val defRes = PreferenceHelper.getString(
requireContext(), requireContext(),
"default_resolution", "default_res",
"hls" ""
)!! )!!
if (defRes != "hls") { if (defRes != "") {
videosNameArray.forEachIndexed { index, pipedStream -> videosNameArray.forEachIndexed { index, pipedStream ->
// search for quality preference in the available stream sources // search for quality preference in the available stream sources
if (pipedStream.contains(defRes)) { if (pipedStream.contains(defRes)) {
@ -1290,9 +1338,12 @@ class PlayerFragment : Fragment() {
) )
.build() .build()
trackSelector = DefaultTrackSelector(requireContext())
exoPlayer = ExoPlayer.Builder(view.context) exoPlayer = ExoPlayer.Builder(view.context)
.setMediaSourceFactory(DefaultMediaSourceFactory(dataSourceFactory)) .setMediaSourceFactory(DefaultMediaSourceFactory(dataSourceFactory))
.setLoadControl(loadControl) .setLoadControl(loadControl)
.setTrackSelector(trackSelector)
.build() .build()
exoPlayer.setAudioAttributes(audioAttributes, true) exoPlayer.setAudioAttributes(audioAttributes, true)

View File

@ -6,18 +6,20 @@ import com.github.libretube.obj.PipedStream
import com.google.android.exoplayer2.ui.CaptionStyleCompat import com.google.android.exoplayer2.ui.CaptionStyleCompat
object PlayerHelper { object PlayerHelper {
private val TAG = "PlayerHelper"
// get the best bit rate from audio streams // get the best bit rate from audio streams
fun getMostBitRate(audios: List<PipedStream>): String { fun getMostBitRate(audios: List<PipedStream>): String {
var bitrate = 0 var bitrate = 0
var index = 0 var audioUrl = ""
for ((i, audio) in audios.withIndex()) { audios.forEach {
val q = audio.quality!!.replace(" kbps", "").toInt() val q = it.quality!!.replace(" kbps", "").toInt()
if (q > bitrate) { if (q > bitrate) {
bitrate = q bitrate = q
index = i audioUrl = it.url.toString()
} }
} }
return audios[index].url!! return audioUrl
} }
// get the system default caption style // get the system default caption style

View File

@ -2,10 +2,9 @@
android:width="24dp" android:width="24dp"
android:height="24dp" android:height="24dp"
android:tint="@android:color/white" android:tint="@android:color/white"
android:viewportWidth="24" android:viewportWidth="48"
android:viewportHeight="24"> android:viewportHeight="48">
<path <path
android:fillColor="#FF000000" android:fillColor="#FF000000"
android:pathData="m20.403,1.197c1.918,0 3.486,1.499 3.595,3.39l0.006,0.212v14.405c0,1.918 -1.499,3.486 -3.39,3.595l-0.212,0.006L3.597,22.803c-1.918,0 -3.486,-1.499 -3.595,-3.39l-0.006,-0.212L-0.004,4.798c0,-1.918 1.499,-3.486 3.39,-3.595l0.212,-0.006zM20.403,3.597L3.597,3.597c-0.616,0 -1.123,0.463 -1.192,1.06l-0.008,0.14v14.405c0,0.616 0.463,1.123 1.06,1.192l0.14,0.008L20.403,20.403c0.616,0 1.123,-0.463 1.192,-1.06l0.008,-0.14L21.603,4.798c0,-0.616 -0.463,-1.123 -1.06,-1.192zM18.002,12c0.616,0 1.123,0.463 1.192,1.06l0.008,0.14v3.601c0,0.616 -0.463,1.123 -1.06,1.192l-0.14,0.008h-3.601c-0.663,0 -1.2,-0.537 -1.2,-1.2 0,-0.616 0.463,-1.123 1.06,-1.192l0.14,-0.008h2.401v-2.401c0,-0.616 0.463,-1.123 1.06,-1.192zM9.599,5.998c0.663,0 1.2,0.537 1.2,1.2 0,0.616 -0.463,1.123 -1.06,1.192L9.599,8.399L7.198,8.399v2.401c0,0.616 -0.463,1.123 -1.06,1.192l-0.14,0.008c-0.616,0 -1.123,-0.463 -1.192,-1.06l-0.008,-0.14L4.798,7.198c0,-0.616 0.463,-1.123 1.06,-1.192L5.998,5.998Z" android:pathData="m28.58,33.834h9.207v-9.352h-2.892v6.46L28.58,30.942ZM10.261,23.518h2.892v-6.46h6.315L19.469,14.166L10.261,14.166ZM7.706,39.715q-1.302,0 -2.29,-0.988Q4.428,37.739 4.428,36.437L4.428,11.563q0,-1.35 0.988,-2.314Q6.405,8.285 7.706,8.285L40.294,8.285q1.35,0 2.314,0.964 0.964,0.964 0.964,2.314v24.874q0,1.302 -0.964,2.29 -0.964,0.988 -2.314,0.988zM7.706,36.437L40.294,36.437L40.294,11.563L7.706,11.563ZM7.706,36.437L7.706,11.563Z" />
android:strokeWidth="1"/>
</vector> </vector>

View File

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="?android:attr/colorControlNormal"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@android:color/white"
android:pathData="M19.5,5.5v13h-15v-13h15zM19,4L5,4c-1.11,0 -2,0.9 -2,2v12c0,1.1 0.89,2 2,2h14c1.1,0 2,-0.9 2,-2L21,6c0,-1.1 -0.9,-2 -2,-2zM11,11L9.5,11v-0.5h-2v3h2L9.5,13L11,13v1c0,0.55 -0.45,1 -1,1L7,15c-0.55,0 -1,-0.45 -1,-1v-4c0,-0.55 0.45,-1 1,-1h3c0.55,0 1,0.45 1,1v1zM18,11h-1.5v-0.5h-2v3h2L16.5,13L18,13v1c0,0.55 -0.45,1 -1,1h-3c-0.55,0 -1,-0.45 -1,-1v-4c0,-0.55 0.45,-1 1,-1h3c0.55,0 1,0.45 1,1v1z" />
</vector>

View File

@ -96,8 +96,10 @@
android:visibility="gone"> android:visibility="gone">
<ImageButton <ImageButton
android:id="@id/exo_subtitle" android:id="@+id/captions"
style="@style/PlayerControlBottom" /> style="@style/PlayerControlTop"
android:src="@drawable/ic_caption_outlined"
app:tint="@android:color/darker_gray" />
<ImageButton <ImageButton
android:id="@+id/repeat_toggle" android:id="@+id/repeat_toggle"

View File

@ -581,7 +581,7 @@
<item>144p</item> <item>144p</item>
</string-array> </string-array>
<string-array name="defresValue"> <string-array name="defresValue">
<item>hls</item> <item></item>
<item>1080p</item> <item>1080p</item>
<item>720p</item> <item>720p</item>
<item>480p</item> <item>480p</item>

View File

@ -250,4 +250,6 @@
<string name="watch_positions_title">Watch positions</string> <string name="watch_positions_title">Watch positions</string>
<string name="reset_watch_positions">Reset watch positions</string> <string name="reset_watch_positions">Reset watch positions</string>
<string name="system_caption_style">System caption style</string> <string name="system_caption_style">System caption style</string>
<string name="captions">Captions</string>
<string name="none">None</string>
</resources> </resources>

View File

@ -6,10 +6,10 @@
<ListPreference <ListPreference
android:icon="@drawable/ic_hd" android:icon="@drawable/ic_hd"
app:defaultValue="hls" app:defaultValue=""
app:entries="@array/defres" app:entries="@array/defres"
app:entryValues="@array/defresValue" app:entryValues="@array/defresValue"
app:key="default_resolution" app:key="default_res"
app:title="@string/defres" app:title="@string/defres"
app:useSimpleSummaryProvider="true" /> app:useSimpleSummaryProvider="true" />