mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-29 08:20:32 +05:30
Merge pull request #6819 from Bnyro/master
fix: don't recreate player on orientation change
This commit is contained in:
commit
48cb60f0b3
@ -57,4 +57,5 @@ object IntentData {
|
|||||||
const val streams = "streams"
|
const val streams = "streams"
|
||||||
const val chapters = "chapters"
|
const val chapters = "chapters"
|
||||||
const val segments = "segments"
|
const val segments = "segments"
|
||||||
|
const val alreadyStarted = "alreadyStarted"
|
||||||
}
|
}
|
||||||
|
@ -116,7 +116,8 @@ object BackgroundHelper {
|
|||||||
fun startMediaService(
|
fun startMediaService(
|
||||||
context: Context,
|
context: Context,
|
||||||
serviceClass: Class<*>,
|
serviceClass: Class<*>,
|
||||||
arguments: Bundle,
|
arguments: Bundle = Bundle.EMPTY,
|
||||||
|
sendStartCommand: Boolean = true,
|
||||||
onController: (MediaController) -> Unit = {}
|
onController: (MediaController) -> Unit = {}
|
||||||
) {
|
) {
|
||||||
val sessionToken =
|
val sessionToken =
|
||||||
@ -126,7 +127,7 @@ object BackgroundHelper {
|
|||||||
MediaController.Builder(context, sessionToken).buildAsync()
|
MediaController.Builder(context, sessionToken).buildAsync()
|
||||||
controllerFuture.addListener({
|
controllerFuture.addListener({
|
||||||
val controller = controllerFuture.get()
|
val controller = controllerFuture.get()
|
||||||
controller.sendCustomCommand(
|
if (sendStartCommand) controller.sendCustomCommand(
|
||||||
AbstractPlayerService.startServiceCommand,
|
AbstractPlayerService.startServiceCommand,
|
||||||
arguments
|
arguments
|
||||||
)
|
)
|
||||||
|
@ -316,10 +316,11 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
val maybeStreams: Streams? = mediaMetadata.extras?.parcelable(IntentData.streams)
|
val maybeStreams: Streams? = mediaMetadata.extras?.parcelable(IntentData.streams)
|
||||||
maybeStreams?.let {
|
maybeStreams?.let { streams ->
|
||||||
streams = it
|
this@PlayerFragment.streams = streams
|
||||||
viewModel.segments.postValue(emptyList())
|
viewModel.segments.postValue(emptyList())
|
||||||
playVideo()
|
setPlayerDefaults()
|
||||||
|
updatePlayerView()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -396,6 +397,10 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
|
|||||||
playlistId = playerData.playlistId
|
playlistId = playerData.playlistId
|
||||||
channelId = playerData.channelId
|
channelId = playerData.channelId
|
||||||
|
|
||||||
|
// remember if playback already started once and only restart playback if that's the first run
|
||||||
|
val createNewSession = !requireArguments().getBoolean(IntentData.alreadyStarted)
|
||||||
|
requireArguments().putBoolean(IntentData.alreadyStarted, true)
|
||||||
|
|
||||||
changeOrientationMode()
|
changeOrientationMode()
|
||||||
|
|
||||||
playerLayoutOrientation = resources.configuration.orientation
|
playerLayoutOrientation = resources.configuration.orientation
|
||||||
@ -425,7 +430,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
|
|||||||
DatabaseHolder.Database.downloadDao().findById(videoId)
|
DatabaseHolder.Database.downloadDao().findById(videoId)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (localDownloadVersion != null) {
|
if (localDownloadVersion != null && createNewSession) {
|
||||||
childFragmentManager.setFragmentResultListener(
|
childFragmentManager.setFragmentResultListener(
|
||||||
PlayOfflineDialog.PLAY_OFFLINE_DIALOG_REQUEST_KEY, viewLifecycleOwner
|
PlayOfflineDialog.PLAY_OFFLINE_DIALOG_REQUEST_KEY, viewLifecycleOwner
|
||||||
) { _, bundle ->
|
) { _, bundle ->
|
||||||
@ -433,7 +438,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
|
|||||||
// offline video playback started and thus the player fragment is no longer needed
|
// offline video playback started and thus the player fragment is no longer needed
|
||||||
killPlayerFragment()
|
killPlayerFragment()
|
||||||
} else {
|
} else {
|
||||||
attachToPlayerService(playerData)
|
attachToPlayerService(playerData, true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -450,15 +455,16 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
|
|||||||
)
|
)
|
||||||
}.show(childFragmentManager, null)
|
}.show(childFragmentManager, null)
|
||||||
} else {
|
} else {
|
||||||
attachToPlayerService(playerData)
|
attachToPlayerService(playerData, createNewSession)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun attachToPlayerService(playerData: PlayerData) {
|
private fun attachToPlayerService(playerData: PlayerData, startNewSession: Boolean) {
|
||||||
BackgroundHelper.startMediaService(
|
BackgroundHelper.startMediaService(
|
||||||
requireContext(),
|
requireContext(),
|
||||||
VideoOnlinePlayerService::class.java,
|
VideoOnlinePlayerService::class.java,
|
||||||
bundleOf(IntentData.playerData to playerData)
|
bundleOf(IntentData.playerData to playerData),
|
||||||
|
sendStartCommand = startNewSession
|
||||||
) {
|
) {
|
||||||
if (_binding == null) {
|
if (_binding == null) {
|
||||||
playerController.sendCustomCommand(
|
playerController.sendCustomCommand(
|
||||||
@ -471,6 +477,19 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
|
|||||||
|
|
||||||
playerController = it
|
playerController = it
|
||||||
playerController.addListener(playerListener)
|
playerController.addListener(playerListener)
|
||||||
|
|
||||||
|
if (!startNewSession) {
|
||||||
|
val streams: Streams? = playerController.mediaMetadata.extras?.parcelable(IntentData.streams)
|
||||||
|
|
||||||
|
// reload the streams data and playback, metadata apparently no longer exists
|
||||||
|
if (streams == null) {
|
||||||
|
playNextVideo(videoId)
|
||||||
|
return@startMediaService
|
||||||
|
}
|
||||||
|
|
||||||
|
this.streams = streams
|
||||||
|
updatePlayerView()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -967,7 +986,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
|
|||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun playVideo() {
|
private fun setPlayerDefaults() {
|
||||||
// reset the player view
|
// reset the player view
|
||||||
playerBinding.exoProgress.clearSegments()
|
playerBinding.exoProgress.clearSegments()
|
||||||
playerBinding.sbToggle.isGone = true
|
playerBinding.sbToggle.isGone = true
|
||||||
@ -993,6 +1012,27 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
|
|||||||
// set media source and resolution in the beginning
|
// set media source and resolution in the beginning
|
||||||
updateResolution(commonPlayerViewModel.isFullscreen.value == true)
|
updateResolution(commonPlayerViewModel.isFullscreen.value == true)
|
||||||
|
|
||||||
|
if (streams.category == Streams.categoryMusic) {
|
||||||
|
playerController.setPlaybackSpeed(1f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Manually skip to another video.
|
||||||
|
*/
|
||||||
|
private fun playNextVideo(nextId: String) {
|
||||||
|
playerController.sendCustomCommand(
|
||||||
|
AbstractPlayerService.runPlayerActionCommand,
|
||||||
|
bundleOf(PlayerCommand.PLAY_VIDEO_BY_ID.name to nextId)
|
||||||
|
)
|
||||||
|
|
||||||
|
// close comment bottom sheet if opened for next video
|
||||||
|
activity?.supportFragmentManager?.fragments?.filterIsInstance<CommentsSheet>()
|
||||||
|
?.firstOrNull()?.dismiss()
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressLint("SetTextI18n")
|
||||||
|
private fun updatePlayerView() {
|
||||||
if (PreferenceHelper.getBoolean(PreferenceKeys.AUTO_FULLSCREEN_SHORTS, false) &&
|
if (PreferenceHelper.getBoolean(PreferenceKeys.AUTO_FULLSCREEN_SHORTS, false) &&
|
||||||
isShort && binding.playerMotionLayout.progress == 0f
|
isShort && binding.playerMotionLayout.progress == 0f
|
||||||
) {
|
) {
|
||||||
@ -1017,8 +1057,6 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
|
|||||||
this
|
this
|
||||||
)
|
)
|
||||||
|
|
||||||
updatePlayerView()
|
|
||||||
|
|
||||||
if (binding.playerMotionLayout.progress != 1.0f) {
|
if (binding.playerMotionLayout.progress != 1.0f) {
|
||||||
// show controllers when not in picture in picture mode
|
// show controllers when not in picture in picture mode
|
||||||
val inPipMode = PlayerHelper.pipEnabled &&
|
val inPipMode = PlayerHelper.pipEnabled &&
|
||||||
@ -1028,29 +1066,8 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (streams.category == Streams.categoryMusic) {
|
|
||||||
playerController.setPlaybackSpeed(1f)
|
|
||||||
}
|
|
||||||
|
|
||||||
viewModel.isOrientationChangeInProgress = false
|
viewModel.isOrientationChangeInProgress = false
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Manually skip to another video.
|
|
||||||
*/
|
|
||||||
private fun playNextVideo(nextId: String) {
|
|
||||||
playerController.sendCustomCommand(
|
|
||||||
AbstractPlayerService.runPlayerActionCommand,
|
|
||||||
bundleOf(PlayerCommand.PLAY_VIDEO_BY_ID.name to nextId)
|
|
||||||
)
|
|
||||||
|
|
||||||
// close comment bottom sheet if opened for next video
|
|
||||||
activity?.supportFragmentManager?.fragments?.filterIsInstance<CommentsSheet>()
|
|
||||||
?.firstOrNull()?.dismiss()
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressLint("SetTextI18n")
|
|
||||||
private fun updatePlayerView() {
|
|
||||||
binding.descriptionLayout.setStreams(streams)
|
binding.descriptionLayout.setStreams(streams)
|
||||||
|
|
||||||
binding.apply {
|
binding.apply {
|
||||||
@ -1438,6 +1455,8 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
|
|||||||
playerLayoutOrientation = orientation
|
playerLayoutOrientation = orientation
|
||||||
|
|
||||||
viewModel.isOrientationChangeInProgress = true
|
viewModel.isOrientationChangeInProgress = true
|
||||||
|
|
||||||
|
playerController.release()
|
||||||
activity?.recreate()
|
activity?.recreate()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user