From d81bbf109490405922eca0286037e6fc43a5d381 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Thu, 22 Dec 2022 10:48:46 +0100 Subject: [PATCH 1/3] use the proper exoplayer api for setting the notification icon --- .../com/github/libretube/util/NowPlayingNotification.kt | 1 + app/src/main/res/values/drawables.xml | 6 ------ 2 files changed, 1 insertion(+), 6 deletions(-) delete mode 100644 app/src/main/res/values/drawables.xml diff --git a/app/src/main/java/com/github/libretube/util/NowPlayingNotification.kt b/app/src/main/java/com/github/libretube/util/NowPlayingNotification.kt index 317c0de5c..28e23fa96 100644 --- a/app/src/main/java/com/github/libretube/util/NowPlayingNotification.kt +++ b/app/src/main/java/com/github/libretube/util/NowPlayingNotification.kt @@ -206,6 +206,7 @@ class NowPlayingNotification( setUseStopAction(true) setColorized(true) setMediaSessionToken(mediaSession.sessionToken) + setSmallIcon(R.drawable.ic_launcher_lockscreen) setUseFastForwardActionInCompactView(true) setUseRewindActionInCompactView(true) } diff --git a/app/src/main/res/values/drawables.xml b/app/src/main/res/values/drawables.xml deleted file mode 100644 index 59fbce367..000000000 --- a/app/src/main/res/values/drawables.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - @drawable/ic_launcher_lockscreen - - \ No newline at end of file From a2296802f0347396749df5a428ab7610c4658ae1 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Thu, 22 Dec 2022 11:17:34 +0100 Subject: [PATCH 2/3] Hide captions in PiP and mini-player --- .../libretube/extensions/UpdateParameters.kt | 6 ++ .../libretube/ui/fragments/PlayerFragment.kt | 72 +++++++------------ .../com/github/libretube/util/PlayerHelper.kt | 4 +- 3 files changed, 35 insertions(+), 47 deletions(-) create mode 100644 app/src/main/java/com/github/libretube/extensions/UpdateParameters.kt diff --git a/app/src/main/java/com/github/libretube/extensions/UpdateParameters.kt b/app/src/main/java/com/github/libretube/extensions/UpdateParameters.kt new file mode 100644 index 000000000..27bb0b302 --- /dev/null +++ b/app/src/main/java/com/github/libretube/extensions/UpdateParameters.kt @@ -0,0 +1,6 @@ +package com.github.libretube.extensions + +import com.google.android.exoplayer2.trackselection.TrackSelector + +fun TrackSelector.updateParameters() { +} diff --git a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt index 5cc2bd483..a037ba0b8 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt @@ -144,6 +144,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 +227,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 +242,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 +250,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 +272,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { triggerId: Int, positive: Boolean, progress: Float - ) { - } + ) {} }) binding.playerMotionLayout.progress = 1.toFloat() @@ -1189,24 +1188,10 @@ 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) { @@ -1215,7 +1200,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { trackSelector.setParameters(params) } - private fun setStreamSource(streams: Streams) { + private fun setStreamSource() { val defaultResolution = PlayerHelper.getDefaultResolution(requireContext()).replace("p", "") if (defaultResolution != "") setPlayerResolution(defaultResolution.toInt()) @@ -1235,7 +1220,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() } @@ -1331,25 +1316,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) } @@ -1415,6 +1384,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,9 +1401,18 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { enableTransition(R.id.yt_transition, true) } binding.linLayout.visibility = View.VISIBLE + + updateCaptionsLanguage(captionLanguage) } } + private fun updateCaptionsLanguage(language: String?) { + val params = trackSelector.buildUponParameters() + .setPreferredTextRoleFlags(C.ROLE_FLAG_CAPTION) + .setPreferredTextLanguage(language) + trackSelector.setParameters(params) + } + fun onUserLeaveHint() { if (usePiP() && shouldStartPiP()) { activity?.enterPictureInPictureMode(getPipParams()) diff --git a/app/src/main/java/com/github/libretube/util/PlayerHelper.kt b/app/src/main/java/com/github/libretube/util/PlayerHelper.kt index 5d0797100..45d7fb076 100644 --- a/app/src/main/java/com/github/libretube/util/PlayerHelper.kt +++ b/app/src/main/java/com/github/libretube/util/PlayerHelper.kt @@ -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] } From 73a7e62e57e9d424ebaa70fcdc064c64a361e1d2 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Thu, 22 Dec 2022 11:24:50 +0100 Subject: [PATCH 3/3] Improve track selector parameters logic --- .../libretube/extensions/UpdateParameters.kt | 8 +++-- .../libretube/ui/fragments/PlayerFragment.kt | 31 ++++++++++--------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/com/github/libretube/extensions/UpdateParameters.kt b/app/src/main/java/com/github/libretube/extensions/UpdateParameters.kt index 27bb0b302..6ce00846d 100644 --- a/app/src/main/java/com/github/libretube/extensions/UpdateParameters.kt +++ b/app/src/main/java/com/github/libretube/extensions/UpdateParameters.kt @@ -1,6 +1,10 @@ package com.github.libretube.extensions -import com.google.android.exoplayer2.trackselection.TrackSelector +import com.google.android.exoplayer2.trackselection.DefaultTrackSelector -fun TrackSelector.updateParameters() { +fun DefaultTrackSelector.updateParameters( + actions: DefaultTrackSelector.Parameters.Builder.() -> Unit +) = apply { + val params = buildUponParameters().apply(actions) + setParameters(params) } diff --git a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt index a037ba0b8..85e373764 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt @@ -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 @@ -1195,9 +1196,10 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { } 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() { @@ -1257,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( - Locale.getDefault().language.lowercase().substring(0, 2) - ) - trackSelector.setParameters(params) + val params = trackSelector.updateParameters { + setPreferredAudioLanguage( + Locale.getDefault().language.lowercase().substring(0, 2) + ) + } exoPlayer = ExoPlayer.Builder(requireContext()) .setMediaSourceFactory(DefaultMediaSourceFactory(dataSourceFactory)) @@ -1355,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) } @@ -1407,10 +1410,10 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { } private fun updateCaptionsLanguage(language: String?) { - val params = trackSelector.buildUponParameters() - .setPreferredTextRoleFlags(C.ROLE_FLAG_CAPTION) - .setPreferredTextLanguage(language) - trackSelector.setParameters(params) + trackSelector.updateParameters { + setPreferredTextRoleFlags(C.ROLE_FLAG_CAPTION) + setPreferredTextLanguage(language) + } } fun onUserLeaveHint() {