WIP - Multiple MediaSourceFactory

This commit is contained in:
CappielloAntonio 2021-11-27 17:07:20 +01:00
parent 72218749c0
commit b30e2651f8
2 changed files with 15 additions and 22 deletions

View file

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

View file

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