From 39d52ff4c26b338729ea838f6e9e53dad8032a54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=81nis?= <97699850+janisslsm@users.noreply.github.com> Date: Thu, 19 Oct 2023 16:06:49 +0300 Subject: [PATCH] fix: downloaded subtitles don't show (#5004) --- .../github/libretube/services/DownloadService.kt | 2 +- .../ui/activities/OfflinePlayerActivity.kt | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/github/libretube/services/DownloadService.kt b/app/src/main/java/com/github/libretube/services/DownloadService.kt index 148fbe1c9..65ed8f31c 100644 --- a/app/src/main/java/com/github/libretube/services/DownloadService.kt +++ b/app/src/main/java/com/github/libretube/services/DownloadService.kt @@ -167,7 +167,7 @@ class DownloadService : LifecycleService() { val url = URL(item.url ?: return) // only fetch the content length if it's not been returned by the API - if (item.downloadSize == 0L) { + if (item.downloadSize <= 0L) { url.getContentLength()?.let { size -> item.downloadSize = size Database.downloadDao().updateDownloadItem(item) 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 48aaeee84..f9b74d6d6 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 @@ -18,6 +18,7 @@ import androidx.media3.datasource.FileDataSource import androidx.media3.exoplayer.ExoPlayer import androidx.media3.exoplayer.source.MergingMediaSource import androidx.media3.exoplayer.source.ProgressiveMediaSource +import androidx.media3.exoplayer.source.SingleSampleMediaSource import androidx.media3.exoplayer.trackselection.DefaultTrackSelector import androidx.media3.ui.PlayerView import com.github.libretube.compat.PictureInPictureCompat @@ -36,10 +37,11 @@ import com.github.libretube.ui.interfaces.TimeFrameReceiver import com.github.libretube.ui.listeners.SeekbarPreviewListener import com.github.libretube.ui.models.PlayerViewModel import com.github.libretube.util.OfflineTimeFrameReceiver -import kotlin.io.path.exists import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext +import kotlin.io.path.exists + @androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class) class OfflinePlayerActivity : BaseActivity() { @@ -156,7 +158,8 @@ class OfflinePlayerActivity : BaseActivity() { private fun setMediaSource(videoUri: Uri?, audioUri: Uri?, subtitleUri: Uri?) { val subtitle = subtitleUri?.let { SubtitleConfiguration.Builder(it) - .setMimeType(MimeTypes.APPLICATION_SUBRIP) + .setMimeType(MimeTypes.APPLICATION_TTML) + .setLanguage("en") .build() } @@ -175,7 +178,13 @@ class OfflinePlayerActivity : BaseActivity() { val audioSource = ProgressiveMediaSource.Factory(FileDataSource.Factory()) .createMediaSource(MediaItem.fromUri(audioUri)) - val mediaSource = MergingMediaSource(audioSource, videoSource) + var mediaSource = MergingMediaSource(audioSource, videoSource) + if (subtitle != null) { + val subtitleSource = SingleSampleMediaSource.Factory(FileDataSource.Factory()) + .createMediaSource(subtitle, C.TIME_UNSET) + + mediaSource = MergingMediaSource(subtitleSource, mediaSource) + } player.setMediaSource(mediaSource) }