From fa9dd8cb5a14ffb341d4af63535d4b9838bb0cdd Mon Sep 17 00:00:00 2001 From: peking_ling Date: Fri, 1 Mar 2024 04:02:48 +0800 Subject: [PATCH] Cache streamed media files on disk (#6927) --- .../service/playback/ExoPlayerWrapper.java | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/ExoPlayerWrapper.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/ExoPlayerWrapper.java index 1900ed7f2..4e29c9b8a 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/playback/ExoPlayerWrapper.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/ExoPlayerWrapper.java @@ -6,9 +6,11 @@ import android.net.Uri; import android.text.TextUtils; import android.util.Log; import android.view.SurfaceHolder; + import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.core.util.Consumer; + import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.DefaultLoadControl; import com.google.android.exoplayer2.DefaultRenderersFactory; @@ -20,6 +22,7 @@ import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.SeekParameters; import com.google.android.exoplayer2.SimpleExoPlayer; import com.google.android.exoplayer2.audio.AudioAttributes; +import com.google.android.exoplayer2.database.ExoDatabaseProvider; import com.google.android.exoplayer2.ext.okhttp.OkHttpDataSource; import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory; import com.google.android.exoplayer2.extractor.mp3.Mp3Extractor; @@ -36,6 +39,10 @@ import com.google.android.exoplayer2.upstream.DataSource; import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory; import com.google.android.exoplayer2.upstream.HttpDataSource; +import com.google.android.exoplayer2.upstream.cache.CacheDataSource; +import com.google.android.exoplayer2.upstream.cache.LeastRecentlyUsedCacheEvictor; +import com.google.android.exoplayer2.upstream.cache.SimpleCache; + import de.danoeh.antennapod.core.ClientConfig; import de.danoeh.antennapod.core.R; import de.danoeh.antennapod.model.feed.VolumeAdaptionSetting; @@ -48,6 +55,7 @@ import io.reactivex.Observable; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.disposables.Disposable; +import java.io.File; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; @@ -70,7 +78,7 @@ public class ExoPlayerWrapper { private Consumer bufferingUpdateListener; private PlaybackParameters playbackParameters; private DefaultTrackSelector trackSelector; - + private SimpleCache simpleCache; @Nullable private LoudnessEnhancer loudnessEnhancer = null; @@ -145,7 +153,8 @@ public class ExoPlayerWrapper { initLoudnessEnhancer(audioSessionId); } }); - + simpleCache = new SimpleCache(new File(context.getCacheDir(), "streaming"), + new LeastRecentlyUsedCacheEvictor(50 * 1024 * 1024), new ExoDatabaseProvider(context)); initLoudnessEnhancer(exoPlayer.getAudioSessionId()); } @@ -182,6 +191,10 @@ public class ExoPlayerWrapper { if (exoPlayer != null) { exoPlayer.release(); } + if (simpleCache != null) { + simpleCache.release(); + simpleCache = null; + } audioSeekCompleteListener = null; audioCompletionListener = null; audioErrorListener = null; @@ -190,6 +203,10 @@ public class ExoPlayerWrapper { public void reset() { exoPlayer.release(); + if (simpleCache != null) { + simpleCache.release(); + simpleCache = null; + } createPlayer(); } @@ -225,6 +242,11 @@ public class ExoPlayerWrapper { httpDataSourceFactory.setDefaultRequestProperties(requestProperties); } DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(context, null, httpDataSourceFactory); + if (s.startsWith("http")) { + dataSourceFactory = new CacheDataSource.Factory() + .setCache(simpleCache) + .setUpstreamDataSourceFactory(httpDataSourceFactory); + } DefaultExtractorsFactory extractorsFactory = new DefaultExtractorsFactory(); extractorsFactory.setConstantBitrateSeekingEnabled(true); extractorsFactory.setMp3ExtractorFlags(Mp3Extractor.FLAG_DISABLE_ID3_METADATA);