From b5e97618ba863c59785535490029e065031f0d85 Mon Sep 17 00:00:00 2001 From: LagradOst <11805592+LagradOst@users.noreply.github.com> Date: Fri, 4 Mar 2022 18:18:05 +0100 Subject: [PATCH] fixed #740 and https://github.com/google/ExoPlayer/issues/9980 --- .../cloudstream3/ui/player/CS3IPlayer.kt | 21 ++++++++-- .../cloudstream3/ui/player/GeneratorPlayer.kt | 40 ++++++++++++++----- .../cloudstream3/ui/player/IPlayer.kt | 1 + 3 files changed, 48 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/player/CS3IPlayer.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/player/CS3IPlayer.kt index 57e1d306..e0b0f9ad 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/player/CS3IPlayer.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/player/CS3IPlayer.kt @@ -40,6 +40,7 @@ class CS3IPlayer : IPlayer { private var isPlaying = false private var exoPlayer: ExoPlayer? = null var cacheSize = 300L * 1024L * 1024L // 300 mb + private var simpleCacheSize = 100L * 1024L * 1024L private val seekActionTime = 30000L @@ -143,13 +144,14 @@ class CS3IPlayer : IPlayer { link: ExtractorLink?, data: ExtractorUri?, startPosition: Long?, - subtitles: Set + subtitles: Set, + subtitle: SubtitleData? ) { Log.i(TAG, "loadPlayer") if (sameEpisode) { saveData() } else { - currentSubtitles = null + currentSubtitles = subtitle playbackPosition = 0 } @@ -420,6 +422,7 @@ class CS3IPlayer : IPlayer { playbackPosition: Long, playBackSpeed: Float, subtitleOffset: Long, + cacheSize: Long, playWhenReady: Boolean = true, cacheFactory: CacheDataSource.Factory? = null, trackSelector: TrackSelector? = null, @@ -446,6 +449,16 @@ class CS3IPlayer : IPlayer { }.toTypedArray() } .setTrackSelector(trackSelector ?: getTrackSelector(context)) + .setLoadControl( + DefaultLoadControl.Builder() + .setTargetBufferBytes(if (cacheSize > Int.MAX_VALUE) Int.MAX_VALUE else cacheSize.toInt()) + .setBufferDurationsMs( + DefaultLoadControl.DEFAULT_MIN_BUFFER_MS, + ((cacheSize * 75L) / 32768L).toInt(), // 500mb = 20min + DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_MS, + DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_AFTER_REBUFFER_MS + ).build() + ) val videoMediaSource = (if (cacheFactory == null) DefaultMediaSourceFactory(context) else DefaultMediaSourceFactory( @@ -465,6 +478,7 @@ class CS3IPlayer : IPlayer { ) setHandleAudioBecomingNoisy(true) setPlaybackSpeed(playBackSpeed) + } } } @@ -550,6 +564,7 @@ class CS3IPlayer : IPlayer { currentWindow, playbackPosition, playBackSpeed, + cacheSize = cacheSize, playWhenReady = isPlaying, // this keep the current state of the player cacheFactory = cacheFactory, subtitleOffset = currentSubtitleOffset @@ -763,7 +778,7 @@ class CS3IPlayer : IPlayer { subtitleHelper.setActiveSubtitles(activeSubtitles.toSet()) if (simpleCache == null) - simpleCache = getCache(context, cacheSize) + simpleCache = getCache(context, simpleCacheSize) val cacheFactory = CacheDataSource.Factory().apply { simpleCache?.let { setCache(it) } diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/player/GeneratorPlayer.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/player/GeneratorPlayer.kt index cb81f80f..76ae2f7e 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/player/GeneratorPlayer.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/player/GeneratorPlayer.kt @@ -123,6 +123,7 @@ class GeneratorPlayer : FullScreenPlayer() { if (isNextEpisode) 0L else getPos() }, currentSubs, + (if(sameEpisode) currentSelectedSubtitles else null) ?: getAutoSelectSubtitle(currentSubs, settings = true, downloads = true), ) } } @@ -433,16 +434,37 @@ class GeneratorPlayer : FullScreenPlayer() { } } - private fun autoSelectFromSettings() { - // auto select subtitle based of settings - val langCode = preferredAutoSelectSubtitles - if (!langCode.isNullOrEmpty() && player.getCurrentPreferredSubtitle() == null) { - val lang = SubtitleHelper.fromTwoLettersToLanguage(langCode) ?: return + private fun getAutoSelectSubtitle( + subtitles: Set, + settings: Boolean, + downloads: Boolean + ): SubtitleData? { + val langCode = preferredAutoSelectSubtitles ?: return null + val lang = SubtitleHelper.fromTwoLettersToLanguage(langCode) ?: return null - currentSubs.firstOrNull { sub -> + if (settings) + subtitles.firstOrNull { sub -> sub.name.startsWith(lang) || sub.name.trim() == langCode }?.let { sub -> + return sub + } + if (downloads) { + return subtitles.firstOrNull { sub -> + (sub.origin == SubtitleOrigin.DOWNLOADED_FILE || sub.name == context?.getString( + R.string.default_subtitles + )) + } + } + return null + } + + private fun autoSelectFromSettings() { + // auto select subtitle based of settings + val langCode = preferredAutoSelectSubtitles + + if (!langCode.isNullOrEmpty() && player.getCurrentPreferredSubtitle() == null) { + getAutoSelectSubtitle(currentSubs, settings = true, downloads = false)?.let { sub -> context?.let { ctx -> if (setSubtitles(sub)) { player.reloadPlayer(ctx) @@ -455,11 +477,7 @@ class GeneratorPlayer : FullScreenPlayer() { private fun autoSelectFromDownloads() { if (player.getCurrentPreferredSubtitle() == null) { - currentSubs.firstOrNull { sub -> - (sub.origin == SubtitleOrigin.DOWNLOADED_FILE || sub.name == context?.getString( - R.string.default_subtitles - )) - }?.let { sub -> + getAutoSelectSubtitle(currentSubs, settings = false, downloads = true)?.let { sub -> context?.let { ctx -> if (setSubtitles(sub)) { player.reloadPlayer(ctx) diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/player/IPlayer.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/player/IPlayer.kt index a3368b3a..dc666f4d 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/player/IPlayer.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/player/IPlayer.kt @@ -95,6 +95,7 @@ interface IPlayer { data: ExtractorUri? = null, startPosition: Long? = null, subtitles : Set, + subtitle : SubtitleData?, ) fun reloadPlayer(context: Context)