From b30e2651f8b5e9128de88dc0187bea9dedd5e411 Mon Sep 17 00:00:00 2001 From: CappielloAntonio Date: Sat, 27 Nov 2021 17:07:20 +0100 Subject: [PATCH] WIP - Multiple MediaSourceFactory --- .../play/service/MultiPlayer.java | 30 +++++++------------ .../play/util/DownloadUtil.java | 7 +++-- 2 files changed, 15 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/com/cappielloantonio/play/service/MultiPlayer.java b/app/src/main/java/com/cappielloantonio/play/service/MultiPlayer.java index 6dd247c9..2b6043e2 100644 --- a/app/src/main/java/com/cappielloantonio/play/service/MultiPlayer.java +++ b/app/src/main/java/com/cappielloantonio/play/service/MultiPlayer.java @@ -8,9 +8,9 @@ import android.widget.Toast; import com.cappielloantonio.play.R; import com.cappielloantonio.play.interfaces.Playback; import com.cappielloantonio.play.model.Song; +import com.cappielloantonio.play.util.CacheUtil; import com.cappielloantonio.play.util.DownloadUtil; import com.cappielloantonio.play.util.MusicUtil; -import com.cappielloantonio.play.util.PreferenceUtil; import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.ExoPlaybackException; import com.google.android.exoplayer2.ExoPlayer; @@ -18,22 +18,18 @@ import com.google.android.exoplayer2.MediaItem; import com.google.android.exoplayer2.Player; 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.source.DefaultMediaSourceFactory; import com.google.android.exoplayer2.upstream.DataSource; 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 java.io.File; - public class MultiPlayer implements Playback { public static final String TAG = MultiPlayer.class.getSimpleName(); private final Context context; private final SimpleExoPlayer exoPlayer; + private final SimpleCache simpleCache; - private SimpleCache simpleCache; private PlaybackCallbacks callbacks; private final ExoPlayer.EventListener eventListener = new ExoPlayer.EventListener() { @@ -94,21 +90,26 @@ public class MultiPlayer implements Playback { public MultiPlayer(Context context) { this.context = context; - setSimpleCache(context); + this.simpleCache = CacheUtil.getCache(context); + + DataSource.Factory downloadDataSourceFactory = new CacheDataSource.Factory() + .setCache(DownloadUtil.getDownloadCache(context)) + .setUpstreamDataSourceFactory(DownloadUtil.getHttpDataSourceFactory()) + .setCacheWriteDataSinkFactory(null); // Disable writing. DataSource.Factory cacheDataSourceFactory = new CacheDataSource.Factory() .setCache(simpleCache) - .setCache(DownloadUtil.getDownloadCache(context)) - .setUpstreamDataSourceFactory(DownloadUtil.getHttpDataSourceFactory(context)); - // .setCacheWriteDataSinkFactory(null); // Disable writing. + .setUpstreamDataSourceFactory(CacheUtil.getHttpDataSourceFactory()); AudioAttributes audioAttributes = new AudioAttributes.Builder() .setUsage(C.USAGE_MEDIA) .setContentType(C.CONTENT_TYPE_MUSIC) .build(); + /* TODO: Capire come affiancare due media source factory */ exoPlayer = new SimpleExoPlayer.Builder(context) .setMediaSourceFactory(new DefaultMediaSourceFactory(cacheDataSourceFactory)) + .setMediaSourceFactory(new DefaultMediaSourceFactory(downloadDataSourceFactory)) .setAudioAttributes(audioAttributes, true) .setHandleAudioBecomingNoisy(true) .setWakeMode(C.WAKE_MODE_NETWORK) @@ -118,15 +119,6 @@ public class MultiPlayer implements Playback { exoPlayer.prepare(); } - private void setSimpleCache(Context context) { - long cacheSize = PreferenceUtil.getInstance(context).getMediaCacheSize(); - LeastRecentlyUsedCacheEvictor recentlyUsedCache = new LeastRecentlyUsedCacheEvictor(cacheSize); - ExoDatabaseProvider databaseProvider = new ExoDatabaseProvider(context); - - File cacheDirectory = new File(context.getCacheDir(), "exoplayer"); - simpleCache = new SimpleCache(cacheDirectory, recentlyUsedCache, databaseProvider); - } - @Override public void setDataSource(Song song) { String uri = MusicUtil.getSongStreamUri(context, song); diff --git a/app/src/main/java/com/cappielloantonio/play/util/DownloadUtil.java b/app/src/main/java/com/cappielloantonio/play/util/DownloadUtil.java index 0177bf6e..b43290d3 100644 --- a/app/src/main/java/com/cappielloantonio/play/util/DownloadUtil.java +++ b/app/src/main/java/com/cappielloantonio/play/util/DownloadUtil.java @@ -39,7 +39,7 @@ public final class DownloadUtil { private static DownloadTracker downloadTracker; private static DownloadNotificationHelper downloadNotificationHelper; - public static synchronized HttpDataSource.Factory getHttpDataSourceFactory(Context context) { + public static synchronized HttpDataSource.Factory getHttpDataSourceFactory() { if (httpDataSourceFactory == null) { CookieManager cookieManager = new CookieManager(); cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ORIGINAL_SERVER); @@ -71,6 +71,7 @@ public final class DownloadUtil { File downloadContentDirectory = new File(getDownloadDirectory(context), DOWNLOAD_CONTENT_DIRECTORY); downloadCache = new SimpleCache(downloadContentDirectory, new NoOpCacheEvictor(), getDatabaseProvider(context)); } + return downloadCache; } @@ -79,8 +80,8 @@ public final class DownloadUtil { DefaultDownloadIndex downloadIndex = new DefaultDownloadIndex(getDatabaseProvider(context)); upgradeActionFile(context, DOWNLOAD_ACTION_FILE, downloadIndex, false); upgradeActionFile(context, DOWNLOAD_TRACKER_ACTION_FILE, downloadIndex, true); - downloadManager = new DownloadManager(context, getDatabaseProvider(context), getDownloadCache(context), getHttpDataSourceFactory(context), Executors.newFixedThreadPool(6)); - downloadTracker = new DownloadTracker(context, getHttpDataSourceFactory(context), downloadManager); + downloadManager = new DownloadManager(context, getDatabaseProvider(context), getDownloadCache(context), getHttpDataSourceFactory(), Executors.newFixedThreadPool(6)); + downloadTracker = new DownloadTracker(context, getHttpDataSourceFactory(), downloadManager); } }