diff --git a/app/src/main/java/com/github/libretube/ui/activities/OfflinePlayerActivity.kt b/app/src/main/java/com/github/libretube/ui/activities/OfflinePlayerActivity.kt index 092b48a78..4a4762fb8 100644 --- a/app/src/main/java/com/github/libretube/ui/activities/OfflinePlayerActivity.kt +++ b/app/src/main/java/com/github/libretube/ui/activities/OfflinePlayerActivity.kt @@ -20,17 +20,22 @@ import com.github.libretube.databinding.ExoStyledPlayerControlViewBinding import com.github.libretube.db.DatabaseHolder.Companion.Database import com.github.libretube.enums.FileType import com.github.libretube.extensions.awaitQuery +import com.github.libretube.extensions.updateParameters import com.github.libretube.ui.base.BaseActivity import com.github.libretube.ui.extensions.setAspectRatio import com.github.libretube.ui.models.PlayerViewModel import com.github.libretube.util.PlayerHelper +import com.google.android.exoplayer2.C import com.google.android.exoplayer2.ExoPlayer import com.google.android.exoplayer2.MediaItem +import com.google.android.exoplayer2.MediaItem.SubtitleConfiguration import com.google.android.exoplayer2.Player import com.google.android.exoplayer2.source.MergingMediaSource import com.google.android.exoplayer2.source.ProgressiveMediaSource +import com.google.android.exoplayer2.trackselection.DefaultTrackSelector import com.google.android.exoplayer2.ui.StyledPlayerView import com.google.android.exoplayer2.upstream.FileDataSource +import com.google.android.exoplayer2.util.MimeTypes import java.io.File class OfflinePlayerActivity : BaseActivity() { @@ -38,6 +43,8 @@ class OfflinePlayerActivity : BaseActivity() { private lateinit var videoId: String private lateinit var player: ExoPlayer private lateinit var playerView: StyledPlayerView + private lateinit var trackSelector: DefaultTrackSelector + private lateinit var playerBinding: ExoStyledPlayerControlViewBinding private val playerViewModel: PlayerViewModel by viewModels() @@ -60,8 +67,11 @@ class OfflinePlayerActivity : BaseActivity() { } private fun initializePlayer() { + trackSelector = DefaultTrackSelector(this) + player = ExoPlayer.Builder(this) .setHandleAudioBecomingNoisy(true) + .setTrackSelector(trackSelector) .build().apply { addListener(object : Player.Listener { override fun onEvents(player: Player, events: Player.Events) { @@ -75,9 +85,9 @@ class OfflinePlayerActivity : BaseActivity() { } playerView = binding.player - + playerView.setShowSubtitleButton(true) + playerView.subtitleView?.visibility = View.VISIBLE playerView.player = player - playerBinding = binding.player.binding playerBinding.fullscreen.visibility = View.GONE @@ -85,11 +95,13 @@ class OfflinePlayerActivity : BaseActivity() { finish() } + PlayerHelper.applyCaptionsStyle(this, playerView.subtitleView) + binding.player.initialize( null, binding.doubleTapOverlay.binding, binding.playerGestureControlsView.binding, - null + trackSelector ) } @@ -110,40 +122,59 @@ class OfflinePlayerActivity : BaseActivity() { val audioUri = audio?.path?.let { File(it).toUri() } val subtitleUri = subtitle?.path?.let { File(it).toUri() } - setMediaSource( - videoUri, - audioUri - ) + setMediaSource(videoUri, audioUri, subtitleUri) + + trackSelector.updateParameters { + setPreferredTextRoleFlags(C.ROLE_FLAG_CAPTION) + setPreferredTextLanguage("en") + } player.prepare() player.play() } - private fun setMediaSource(videoUri: Uri?, audioUri: Uri?) { + private fun setMediaSource(videoUri: Uri?, audioUri: Uri?, subtitleUri: Uri?) { + val subtitle = subtitleUri?.let { + SubtitleConfiguration.Builder(it) + .setMimeType(MimeTypes.APPLICATION_SUBRIP) + .build() + } + subtitle?.id + when { videoUri != null && audioUri != null -> { + val videoItem = MediaItem.Builder() + .setUri(videoUri) + .apply { + if (subtitle != null) setSubtitleConfigurations(listOf(subtitle)) + } + .build() + val videoSource = ProgressiveMediaSource.Factory(FileDataSource.Factory()) - .createMediaSource( - MediaItem.fromUri(videoUri) - ) + .createMediaSource(videoItem) val audioSource = ProgressiveMediaSource.Factory(FileDataSource.Factory()) - .createMediaSource( - MediaItem.fromUri(audioUri) - ) + .createMediaSource(MediaItem.fromUri(audioUri)) - val mediaSource = MergingMediaSource( - audioSource, - videoSource - ) + val mediaSource = MergingMediaSource(audioSource, videoSource) player.setMediaSource(mediaSource) } videoUri != null -> player.setMediaItem( - MediaItem.fromUri(videoUri) + MediaItem.Builder() + .setUri(videoUri) + .apply { + if (subtitle != null) setSubtitleConfigurations(listOf(subtitle)) + } + .build() ) audioUri != null -> player.setMediaItem( - MediaItem.fromUri(audioUri) + MediaItem.Builder() + .setUri(audioUri) + .apply { + if (subtitle != null) setSubtitleConfigurations(listOf(subtitle)) + } + .build() ) } } diff --git a/app/src/main/java/com/github/libretube/ui/dialogs/DownloadDialog.kt b/app/src/main/java/com/github/libretube/ui/dialogs/DownloadDialog.kt index 0ca9b30be..0a0ac45cf 100644 --- a/app/src/main/java/com/github/libretube/ui/dialogs/DownloadDialog.kt +++ b/app/src/main/java/com/github/libretube/ui/dialogs/DownloadDialog.kt @@ -31,14 +31,6 @@ class DownloadDialog( fetchAvailableSources() - binding.audioRadio.setOnClickListener { - binding.videoSpinner.visibility = View.GONE - } - - binding.videoRadio.setOnClickListener { - binding.videoSpinner.visibility = View.VISIBLE - } - binding.fileName.filters += InputFilter { source, start, end, _, _, _ -> if (source.isNullOrBlank()) { return@InputFilter null 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 fe73fc124..2845a5dc8 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 @@ -103,9 +103,7 @@ import com.google.android.exoplayer2.Player import com.google.android.exoplayer2.audio.AudioAttributes import com.google.android.exoplayer2.ext.cronet.CronetDataSource import com.google.android.exoplayer2.source.DefaultMediaSourceFactory -import com.google.android.exoplayer2.text.Cue.TEXT_SIZE_TYPE_ABSOLUTE import com.google.android.exoplayer2.trackselection.DefaultTrackSelector -import com.google.android.exoplayer2.ui.CaptionStyleCompat import com.google.android.exoplayer2.ui.StyledPlayerView import com.google.android.exoplayer2.upstream.DefaultDataSource import com.google.android.exoplayer2.util.MimeTypes @@ -816,7 +814,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { private fun prepareExoPlayerView() { exoPlayerView.apply { - setShowSubtitleButton(true) + setShowSubtitleButton(false) setShowNextButton(false) setShowPreviousButton(false) // controllerShowTimeoutMs = 1500 @@ -827,18 +825,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { playerBinding.exoProgress.setPlayer(exoPlayer) - applyCaptionStyle() - } - - private fun applyCaptionStyle() { - val captionStyle = PlayerHelper.getCaptionStyle(requireContext()) - exoPlayerView.subtitleView?.apply { - setApplyEmbeddedFontSizes(false) - setFixedTextSize(TEXT_SIZE_TYPE_ABSOLUTE, PlayerHelper.captionsTextSize) - if (!PlayerHelper.useSystemCaptionStyle) return - setApplyEmbeddedStyles(captionStyle == CaptionStyleCompat.DEFAULT) - setStyle(captionStyle) - } + PlayerHelper.applyCaptionsStyle(requireContext(), exoPlayerView.subtitleView) } private fun localizedDate(date: String?): String? { 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 3687d7aed..560021075 100644 --- a/app/src/main/java/com/github/libretube/util/PlayerHelper.kt +++ b/app/src/main/java/com/github/libretube/util/PlayerHelper.kt @@ -16,7 +16,9 @@ import com.github.libretube.api.obj.PipedStream import com.github.libretube.constants.PreferenceKeys import com.github.libretube.enums.AudioQuality import com.github.libretube.enums.PlayerEvent +import com.google.android.exoplayer2.text.Cue import com.google.android.exoplayer2.ui.CaptionStyleCompat +import com.google.android.exoplayer2.ui.SubtitleView import com.google.android.exoplayer2.video.VideoSize import kotlin.math.roundToInt @@ -442,4 +444,18 @@ object PlayerHelper { ) return actions } + + /** + * Load the captions style according to the users preferences + */ + fun applyCaptionsStyle(context: Context, subtitleView: SubtitleView?) { + val captionStyle = getCaptionStyle(context) + subtitleView?.apply { + setApplyEmbeddedFontSizes(false) + setFixedTextSize(Cue.TEXT_SIZE_TYPE_ABSOLUTE, captionsTextSize) + if (!useSystemCaptionStyle) return + setApplyEmbeddedStyles(captionStyle == CaptionStyleCompat.DEFAULT) + setStyle(captionStyle) + } + } } diff --git a/app/src/main/res/layout/dialog_download.xml b/app/src/main/res/layout/dialog_download.xml index ebfbcf6d3..7867c9b9d 100644 --- a/app/src/main/res/layout/dialog_download.xml +++ b/app/src/main/res/layout/dialog_download.xml @@ -25,27 +25,6 @@ android:inputType="text" /> - - - - - - - -