mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 06:10:31 +05:30
Merge pull request #801 from Bnyro/master
Option to use system default caption style
This commit is contained in:
commit
4c6c60c808
@ -46,7 +46,6 @@ import com.github.libretube.dialogs.AddtoPlaylistDialog
|
||||
import com.github.libretube.dialogs.DownloadDialog
|
||||
import com.github.libretube.dialogs.ShareDialog
|
||||
import com.github.libretube.obj.ChapterSegment
|
||||
import com.github.libretube.obj.PipedStream
|
||||
import com.github.libretube.obj.Playlist
|
||||
import com.github.libretube.obj.Segment
|
||||
import com.github.libretube.obj.Segments
|
||||
@ -60,6 +59,7 @@ import com.github.libretube.util.BackgroundMode
|
||||
import com.github.libretube.util.ConnectionHelper
|
||||
import com.github.libretube.util.CronetHelper
|
||||
import com.github.libretube.util.DescriptionAdapter
|
||||
import com.github.libretube.util.PlayerHelper
|
||||
import com.github.libretube.util.RetrofitInstance
|
||||
import com.github.libretube.util.formatShort
|
||||
import com.github.libretube.views.DoubleClickListener
|
||||
@ -78,6 +78,7 @@ import com.google.android.exoplayer2.source.MediaSource
|
||||
import com.google.android.exoplayer2.source.MergingMediaSource
|
||||
import com.google.android.exoplayer2.source.ProgressiveMediaSource
|
||||
import com.google.android.exoplayer2.ui.AspectRatioFrameLayout
|
||||
import com.google.android.exoplayer2.ui.CaptionStyleCompat
|
||||
import com.google.android.exoplayer2.ui.PlayerNotificationManager
|
||||
import com.google.android.exoplayer2.ui.StyledPlayerView
|
||||
import com.google.android.exoplayer2.ui.TimeBar
|
||||
@ -349,6 +350,60 @@ class PlayerFragment : Fragment() {
|
||||
isPlayerLocked = !isPlayerLocked
|
||||
}
|
||||
|
||||
// set default playback speed
|
||||
val playbackSpeed =
|
||||
PreferenceHelper.getString(requireContext(), "playback_speed", "1F")!!
|
||||
val playbackSpeeds = context?.resources?.getStringArray(R.array.playbackSpeed)!!
|
||||
val playbackSpeedValues =
|
||||
context?.resources?.getStringArray(R.array.playbackSpeedValues)!!
|
||||
exoPlayer.setPlaybackSpeed(playbackSpeed.toFloat())
|
||||
val speedIndex = playbackSpeedValues.indexOf(playbackSpeed)
|
||||
playerBinding.speedText.text = playbackSpeeds[speedIndex]
|
||||
|
||||
// change playback speed button
|
||||
playerBinding.speedText.setOnClickListener {
|
||||
MaterialAlertDialogBuilder(requireContext())
|
||||
.setTitle(R.string.change_playback_speed)
|
||||
.setItems(playbackSpeeds) { _, index ->
|
||||
// set the new playback speed
|
||||
val newPlaybackSpeed = playbackSpeedValues[index].toFloat()
|
||||
exoPlayer.setPlaybackSpeed(newPlaybackSpeed)
|
||||
playerBinding.speedText.text = playbackSpeeds[index]
|
||||
}
|
||||
.show()
|
||||
}
|
||||
|
||||
// repeat toggle button
|
||||
playerBinding.repeatToggle.setOnClickListener {
|
||||
if (exoPlayer.repeatMode == RepeatModeUtil.REPEAT_TOGGLE_MODE_ALL) {
|
||||
// turn off repeat mode
|
||||
exoPlayer.repeatMode = RepeatModeUtil.REPEAT_TOGGLE_MODE_NONE
|
||||
playerBinding.repeatToggle.setColorFilter(Color.GRAY)
|
||||
} else {
|
||||
exoPlayer.repeatMode = RepeatModeUtil.REPEAT_TOGGLE_MODE_ALL
|
||||
playerBinding.repeatToggle.setColorFilter(Color.WHITE)
|
||||
}
|
||||
}
|
||||
|
||||
// share button
|
||||
binding.relPlayerShare.setOnClickListener {
|
||||
val shareDialog = ShareDialog(videoId!!, false)
|
||||
shareDialog.show(childFragmentManager, "ShareDialog")
|
||||
}
|
||||
|
||||
binding.relPlayerBackground.setOnClickListener {
|
||||
// pause the current player
|
||||
exoPlayer.pause()
|
||||
|
||||
// start the background mode
|
||||
BackgroundMode
|
||||
.getInstance()
|
||||
.playOnBackgroundMode(
|
||||
requireContext(),
|
||||
videoId!!
|
||||
)
|
||||
}
|
||||
|
||||
binding.playerScrollView.viewTreeObserver
|
||||
.addOnScrollChangedListener {
|
||||
if (binding.playerScrollView.getChildAt(0).bottom
|
||||
@ -738,6 +793,14 @@ class PlayerFragment : Fragment() {
|
||||
useController = false
|
||||
player = exoPlayer
|
||||
}
|
||||
|
||||
val useSystemCaptionStyle = PreferenceHelper.getBoolean(requireContext(), "system_caption_style", true)
|
||||
if (useSystemCaptionStyle) {
|
||||
// set the subtitle style
|
||||
val captionStyle = PlayerHelper.getCaptionStyle(requireContext())
|
||||
exoPlayerView.subtitleView?.setApplyEmbeddedStyles(captionStyle == CaptionStyleCompat.DEFAULT)
|
||||
exoPlayerView.subtitleView?.setStyle(captionStyle)
|
||||
}
|
||||
}
|
||||
|
||||
private fun initializePlayerView(view: View, response: Streams) {
|
||||
@ -766,29 +829,6 @@ class PlayerFragment : Fragment() {
|
||||
initializeChapters()
|
||||
}
|
||||
|
||||
// set default playback speed
|
||||
val playbackSpeed =
|
||||
PreferenceHelper.getString(requireContext(), "playback_speed", "1F")!!
|
||||
val playbackSpeeds = context?.resources?.getStringArray(R.array.playbackSpeed)!!
|
||||
val playbackSpeedValues =
|
||||
context?.resources?.getStringArray(R.array.playbackSpeedValues)!!
|
||||
exoPlayer.setPlaybackSpeed(playbackSpeed.toFloat())
|
||||
val speedIndex = playbackSpeedValues.indexOf(playbackSpeed)
|
||||
playerBinding.speedText.text = playbackSpeeds[speedIndex]
|
||||
|
||||
// change playback speed button
|
||||
playerBinding.speedText.setOnClickListener {
|
||||
MaterialAlertDialogBuilder(requireContext())
|
||||
.setTitle(R.string.change_playback_speed)
|
||||
.setItems(playbackSpeeds) { _, index ->
|
||||
// set the new playback speed
|
||||
val newPlaybackSpeed = playbackSpeedValues[index].toFloat()
|
||||
exoPlayer.setPlaybackSpeed(newPlaybackSpeed)
|
||||
playerBinding.speedText.text = playbackSpeeds[index]
|
||||
}
|
||||
.show()
|
||||
}
|
||||
|
||||
// Listener for play and pause icon change
|
||||
exoPlayer.addListener(object : Player.Listener {
|
||||
override fun onIsPlayingChanged(isPlaying: Boolean) {
|
||||
@ -855,37 +895,6 @@ class PlayerFragment : Fragment() {
|
||||
}
|
||||
})
|
||||
|
||||
// repeat toggle button
|
||||
playerBinding.repeatToggle.setOnClickListener {
|
||||
if (exoPlayer.repeatMode == RepeatModeUtil.REPEAT_TOGGLE_MODE_ALL) {
|
||||
// turn off repeat mode
|
||||
exoPlayer.repeatMode = RepeatModeUtil.REPEAT_TOGGLE_MODE_NONE
|
||||
playerBinding.repeatToggle.setColorFilter(Color.GRAY)
|
||||
} else {
|
||||
exoPlayer.repeatMode = RepeatModeUtil.REPEAT_TOGGLE_MODE_ALL
|
||||
playerBinding.repeatToggle.setColorFilter(Color.WHITE)
|
||||
}
|
||||
}
|
||||
|
||||
// share button
|
||||
binding.relPlayerShare.setOnClickListener {
|
||||
val shareDialog = ShareDialog(videoId!!, false)
|
||||
shareDialog.show(childFragmentManager, "ShareDialog")
|
||||
}
|
||||
|
||||
binding.relPlayerBackground.setOnClickListener {
|
||||
// pause the current player
|
||||
exoPlayer.pause()
|
||||
|
||||
// start the background mode
|
||||
BackgroundMode
|
||||
.getInstance()
|
||||
.playOnBackgroundMode(
|
||||
requireContext(),
|
||||
videoId!!
|
||||
)
|
||||
}
|
||||
|
||||
// check if livestream
|
||||
if (response.duration!! > 0) {
|
||||
// download clicked
|
||||
@ -1195,7 +1204,7 @@ class PlayerFragment : Fragment() {
|
||||
exoPlayer.setMediaItem(mediaItem)
|
||||
} else {
|
||||
val videoUri = videosUrlArray[which]
|
||||
val audioUrl = getMostBitRate(response.audioStreams!!)
|
||||
val audioUrl = PlayerHelper.getMostBitRate(response.audioStreams!!)
|
||||
setMediaSource(videoUri, audioUrl)
|
||||
}
|
||||
exoPlayer.seekTo(lastPosition)
|
||||
@ -1222,7 +1231,7 @@ class PlayerFragment : Fragment() {
|
||||
// search for quality preference in the available stream sources
|
||||
if (pipedStream.contains(defRes)) {
|
||||
val videoUri = videosUrlArray[index]
|
||||
val audioUrl = getMostBitRate(streams.audioStreams!!)
|
||||
val audioUrl = PlayerHelper.getMostBitRate(streams.audioStreams!!)
|
||||
setMediaSource(videoUri, audioUrl)
|
||||
playerBinding.qualityText.text = videosNameArray[index]
|
||||
return
|
||||
@ -1244,7 +1253,7 @@ class PlayerFragment : Fragment() {
|
||||
// if nothing found, use the first list entry
|
||||
if (videosUrlArray.isNotEmpty()) {
|
||||
val videoUri = videosUrlArray[0]
|
||||
val audioUrl = getMostBitRate(streams.audioStreams!!)
|
||||
val audioUrl = PlayerHelper.getMostBitRate(streams.audioStreams!!)
|
||||
setMediaSource(videoUri, audioUrl)
|
||||
playerBinding.qualityText.text = videosNameArray[0]
|
||||
}
|
||||
@ -1430,19 +1439,6 @@ class PlayerFragment : Fragment() {
|
||||
activity?.runOnUiThread(action)
|
||||
}
|
||||
|
||||
private fun getMostBitRate(audios: List<PipedStream>): String {
|
||||
var bitrate = 0
|
||||
var index = 0
|
||||
for ((i, audio) in audios.withIndex()) {
|
||||
val q = audio.quality!!.replace(" kbps", "").toInt()
|
||||
if (q > bitrate) {
|
||||
bitrate = q
|
||||
index = i
|
||||
}
|
||||
}
|
||||
return audios[index].url!!
|
||||
}
|
||||
|
||||
private fun fetchComments() {
|
||||
lifecycleScope.launchWhenCreated {
|
||||
val commentsResponse = try {
|
||||
|
34
app/src/main/java/com/github/libretube/util/PlayerHelper.kt
Normal file
34
app/src/main/java/com/github/libretube/util/PlayerHelper.kt
Normal file
@ -0,0 +1,34 @@
|
||||
package com.github.libretube.util
|
||||
|
||||
import android.content.Context
|
||||
import android.view.accessibility.CaptioningManager
|
||||
import com.github.libretube.obj.PipedStream
|
||||
import com.google.android.exoplayer2.ui.CaptionStyleCompat
|
||||
|
||||
object PlayerHelper {
|
||||
// get the best bit rate from audio streams
|
||||
fun getMostBitRate(audios: List<PipedStream>): String {
|
||||
var bitrate = 0
|
||||
var index = 0
|
||||
for ((i, audio) in audios.withIndex()) {
|
||||
val q = audio.quality!!.replace(" kbps", "").toInt()
|
||||
if (q > bitrate) {
|
||||
bitrate = q
|
||||
index = i
|
||||
}
|
||||
}
|
||||
return audios[index].url!!
|
||||
}
|
||||
|
||||
// get the system default caption style
|
||||
fun getCaptionStyle(context: Context): CaptionStyleCompat {
|
||||
val captioningManager = context.getSystemService(Context.CAPTIONING_SERVICE) as CaptioningManager
|
||||
return if (!captioningManager.isEnabled) {
|
||||
// system captions are disabled, using android default captions style
|
||||
CaptionStyleCompat.DEFAULT
|
||||
} else {
|
||||
// system captions are enabled
|
||||
CaptionStyleCompat.createFromCaptionStyle(captioningManager.userStyle)
|
||||
}
|
||||
}
|
||||
}
|
10
app/src/main/res/drawable/ic_caption.xml
Normal file
10
app/src/main/res/drawable/ic_caption.xml
Normal 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,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>
|
@ -249,4 +249,5 @@
|
||||
<string name="history_summary">Watch and search history</string>
|
||||
<string name="watch_positions_title">Watch positions</string>
|
||||
<string name="reset_watch_positions">Reset watch positions</string>
|
||||
<string name="system_caption_style">System caption style</string>
|
||||
</resources>
|
@ -78,6 +78,16 @@
|
||||
|
||||
</PreferenceCategory>
|
||||
|
||||
<PreferenceCategory app:title="@string/player">
|
||||
|
||||
<SwitchPreferenceCompat
|
||||
android:icon="@drawable/ic_caption"
|
||||
app:key="system_caption_style"
|
||||
app:title="@string/system_caption_style"
|
||||
app:defaultValue="true" />
|
||||
|
||||
</PreferenceCategory>
|
||||
|
||||
<PreferenceCategory>
|
||||
|
||||
<SwitchPreferenceCompat
|
||||
|
Loading…
Reference in New Issue
Block a user