Cache streamed media files on disk (#6927)

This commit is contained in:
peking_ling 2024-03-01 04:02:48 +08:00 committed by GitHub
parent 33569e8992
commit fa9dd8cb5a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 24 additions and 2 deletions

View File

@ -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<Integer> 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);