Merge pull request #2476 from Bnyro/master

Hide captions in PiP and mini-player
This commit is contained in:
Bnyro 2022-12-22 11:25:01 +01:00 committed by GitHub
commit 1cbf60ae68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 53 additions and 63 deletions

View File

@ -0,0 +1,10 @@
package com.github.libretube.extensions
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector
fun DefaultTrackSelector.updateParameters(
actions: DefaultTrackSelector.Parameters.Builder.() -> Unit
) = apply {
val params = buildUponParameters().apply(actions)
setParameters(params)
}

View File

@ -59,6 +59,7 @@ import com.github.libretube.extensions.hideKeyboard
import com.github.libretube.extensions.query import com.github.libretube.extensions.query
import com.github.libretube.extensions.toID import com.github.libretube.extensions.toID
import com.github.libretube.extensions.toStreamItem import com.github.libretube.extensions.toStreamItem
import com.github.libretube.extensions.updateParameters
import com.github.libretube.obj.ShareData import com.github.libretube.obj.ShareData
import com.github.libretube.obj.VideoResolution import com.github.libretube.obj.VideoResolution
import com.github.libretube.services.BackgroundMode import com.github.libretube.services.BackgroundMode
@ -144,6 +145,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
*/ */
private lateinit var exoPlayer: ExoPlayer private lateinit var exoPlayer: ExoPlayer
private lateinit var trackSelector: DefaultTrackSelector private lateinit var trackSelector: DefaultTrackSelector
private var captionLanguage: String? = PlayerHelper.defaultSubtitleCode
/** /**
* Chapters and comments * Chapters and comments
@ -226,14 +228,14 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
private fun initializeTransitionLayout() { private fun initializeTransitionLayout() {
val mainActivity = activity as MainActivity val mainActivity = activity as MainActivity
mainActivity.binding.container.visibility = View.VISIBLE mainActivity.binding.container.visibility = View.VISIBLE
val mainMotionLayout = mainActivity.binding.mainMotionLayout
binding.playerMotionLayout.addTransitionListener(object : MotionLayout.TransitionListener { binding.playerMotionLayout.addTransitionListener(object : MotionLayout.TransitionListener {
override fun onTransitionStarted( override fun onTransitionStarted(
motionLayout: MotionLayout?, motionLayout: MotionLayout?,
startId: Int, startId: Int,
endId: Int endId: Int
) { ) {}
}
override fun onTransitionChange( override fun onTransitionChange(
motionLayout: MotionLayout?, motionLayout: MotionLayout?,
@ -241,8 +243,6 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
endId: Int, endId: Int,
progress: Float progress: Float
) { ) {
val mainMotionLayout =
mainActivity.binding.mainMotionLayout
mainMotionLayout.progress = abs(progress) mainMotionLayout.progress = abs(progress)
exoPlayerView.hideController() exoPlayerView.hideController()
exoPlayerView.useController = false exoPlayerView.useController = false
@ -251,16 +251,17 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
} }
override fun onTransitionCompleted(motionLayout: MotionLayout?, currentId: Int) { override fun onTransitionCompleted(motionLayout: MotionLayout?, currentId: Int) {
println(currentId)
val mainMotionLayout =
mainActivity.binding.mainMotionLayout
if (currentId == eId) { if (currentId == eId) {
viewModel.isMiniPlayerVisible.value = true viewModel.isMiniPlayerVisible.value = true
// disable captions
updateCaptionsLanguage(null)
exoPlayerView.useController = false exoPlayerView.useController = false
mainMotionLayout.progress = 1F mainMotionLayout.progress = 1F
(activity as MainActivity).requestOrientationChange() (activity as MainActivity).requestOrientationChange()
} else if (currentId == sId) { } else if (currentId == sId) {
viewModel.isMiniPlayerVisible.value = false viewModel.isMiniPlayerVisible.value = false
// re-enable captions
updateCaptionsLanguage(captionLanguage)
exoPlayerView.useController = true exoPlayerView.useController = true
mainMotionLayout.progress = 0F mainMotionLayout.progress = 0F
changeOrientationMode() changeOrientationMode()
@ -272,8 +273,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
triggerId: Int, triggerId: Int,
positive: Boolean, positive: Boolean,
progress: Float progress: Float
) { ) {}
}
}) })
binding.playerMotionLayout.progress = 1.toFloat() binding.playerMotionLayout.progress = 1.toFloat()
@ -1189,33 +1189,20 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
} }
// set the default subtitle if available // set the default subtitle if available
val newParams = trackSelector.buildUponParameters() updateCaptionsLanguage(captionLanguage)
if (PlayerHelper.defaultSubtitleCode != "" && subtitleCodesList.contains(
PlayerHelper.defaultSubtitleCode
)
) {
newParams
.setPreferredTextLanguage(PlayerHelper.defaultSubtitleCode)
.setPreferredTextRoleFlags(C.ROLE_FLAG_CAPTION)
} else {
newParams.setPreferredTextLanguage(null)
}
trackSelector.setParameters(newParams)
// set media source and resolution in the beginning // set media source and resolution in the beginning
setStreamSource( setStreamSource()
streams
)
} }
private fun setPlayerResolution(resolution: Int) { private fun setPlayerResolution(resolution: Int) {
val params = trackSelector.buildUponParameters() trackSelector.updateParameters {
params.setMaxVideoSize(Int.MAX_VALUE, resolution).setMinVideoSize(Int.MIN_VALUE, resolution) setMaxVideoSize(Int.MAX_VALUE, resolution)
trackSelector.setParameters(params) setMinVideoSize(Int.MIN_VALUE, resolution)
}
} }
private fun setStreamSource(streams: Streams) { private fun setStreamSource() {
val defaultResolution = PlayerHelper.getDefaultResolution(requireContext()).replace("p", "") val defaultResolution = PlayerHelper.getDefaultResolution(requireContext()).replace("p", "")
if (defaultResolution != "") setPlayerResolution(defaultResolution.toInt()) if (defaultResolution != "") setPlayerResolution(defaultResolution.toInt())
@ -1235,7 +1222,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
this.setMediaSource(uri, MimeTypes.APPLICATION_MPD) this.setMediaSource(uri, MimeTypes.APPLICATION_MPD)
} else if (streams.hls != null) { } else if (streams.hls != null) {
setMediaSource(streams.hls.toUri(), MimeTypes.APPLICATION_M3U8) setMediaSource(streams.hls!!.toUri(), MimeTypes.APPLICATION_M3U8)
} else { } else {
Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show() Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show()
} }
@ -1272,10 +1259,11 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
// control for the track sources like subtitles and audio source // control for the track sources like subtitles and audio source
trackSelector = DefaultTrackSelector(requireContext()) trackSelector = DefaultTrackSelector(requireContext())
val params = trackSelector.buildUponParameters().setPreferredAudioLanguage( val params = trackSelector.updateParameters {
Locale.getDefault().language.lowercase().substring(0, 2) setPreferredAudioLanguage(
) Locale.getDefault().language.lowercase().substring(0, 2)
trackSelector.setParameters(params) )
}
exoPlayer = ExoPlayer.Builder(requireContext()) exoPlayer = ExoPlayer.Builder(requireContext())
.setMediaSourceFactory(DefaultMediaSourceFactory(dataSourceFactory)) .setMediaSourceFactory(DefaultMediaSourceFactory(dataSourceFactory))
@ -1331,25 +1319,9 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
BaseBottomSheet() BaseBottomSheet()
.setSimpleItems(subtitlesNamesList) { index -> .setSimpleItems(subtitlesNamesList) { index ->
val newParams = if (index != 0) { val language = if (index > 0) subtitleCodesList[index] else null
// caption selected updateCaptionsLanguage(language)
this.captionLanguage = language
// get the caption language code
val captionLanguageCode = subtitleCodesList[index]
// select the new caption preference
trackSelector.buildUponParameters()
.setPreferredTextLanguage(captionLanguageCode)
.setPreferredTextRoleFlags(C.ROLE_FLAG_CAPTION)
} else {
// none selected
// disable captions
trackSelector.buildUponParameters()
.setPreferredTextLanguage(null)
}
// set the new caption language
trackSelector.setParameters(newParams)
} }
.show(childFragmentManager) .show(childFragmentManager)
} }
@ -1386,9 +1358,9 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
.setSimpleItems(audioLanguages) { index -> .setSimpleItems(audioLanguages) { index ->
val audioStreams = audioGroups.values.elementAt(index) val audioStreams = audioGroups.values.elementAt(index)
val lang = audioStreams.firstOrNull()?.audioTrackId?.substring(0, 2) val lang = audioStreams.firstOrNull()?.audioTrackId?.substring(0, 2)
val newParams = trackSelector.buildUponParameters() trackSelector.updateParameters {
.setPreferredAudioLanguage(lang) setPreferredAudioLanguage(lang)
trackSelector.setParameters(newParams) }
} }
.show(childFragmentManager) .show(childFragmentManager)
} }
@ -1415,6 +1387,8 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
binding.linLayout.visibility = View.GONE binding.linLayout.visibility = View.GONE
viewModel.isFullscreen.value = false viewModel.isFullscreen.value = false
updateCaptionsLanguage(null)
} else if (lifecycle.currentState == Lifecycle.State.CREATED) { } else if (lifecycle.currentState == Lifecycle.State.CREATED) {
// close button got clicked in PiP mode // close button got clicked in PiP mode
// destroying the fragment, player and notification // destroying the fragment, player and notification
@ -1430,6 +1404,15 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
enableTransition(R.id.yt_transition, true) enableTransition(R.id.yt_transition, true)
} }
binding.linLayout.visibility = View.VISIBLE binding.linLayout.visibility = View.VISIBLE
updateCaptionsLanguage(captionLanguage)
}
}
private fun updateCaptionsLanguage(language: String?) {
trackSelector.updateParameters {
setPreferredTextRoleFlags(C.ROLE_FLAG_CAPTION)
setPreferredTextLanguage(language)
} }
} }

View File

@ -206,6 +206,7 @@ class NowPlayingNotification(
setUseStopAction(true) setUseStopAction(true)
setColorized(true) setColorized(true)
setMediaSessionToken(mediaSession.sessionToken) setMediaSessionToken(mediaSession.sessionToken)
setSmallIcon(R.drawable.ic_launcher_lockscreen)
setUseFastForwardActionInCompactView(true) setUseFastForwardActionInCompactView(true)
setUseRewindActionInCompactView(true) setUseRewindActionInCompactView(true)
} }

View File

@ -225,13 +225,15 @@ object PlayerHelper {
true true
) )
val defaultSubtitleCode: String val defaultSubtitleCode: String?
get() { get() {
val code = PreferenceHelper.getString( val code = PreferenceHelper.getString(
PreferenceKeys.DEFAULT_SUBTITLE, PreferenceKeys.DEFAULT_SUBTITLE,
"" ""
) )
if (code == "") return null
if (code.contains("-")) { if (code.contains("-")) {
return code.split("-")[0] return code.split("-")[0]
} }

View File

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="utf-8"?><!-- Replaces the drawables of the default exoplayer controls (see https://github.com/google/ExoPlayer/blob/release-v2/library/ui/src/main/res/values/drawables.xml) -->
<resources>
<drawable name="exo_notification_small_icon">@drawable/ic_launcher_lockscreen</drawable>
</resources>