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

View File

@ -225,13 +225,15 @@ object PlayerHelper {
true
)
val defaultSubtitleCode: String
val defaultSubtitleCode: String?
get() {
val code = PreferenceHelper.getString(
PreferenceKeys.DEFAULT_SUBTITLE,
""
)
if (code == "") return null
if (code.contains("-")) {
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>