From 5969115062e5270839184d251af7df9768df7da3 Mon Sep 17 00:00:00 2001 From: Page Asgardius Date: Tue, 4 Oct 2022 12:00:37 -0700 Subject: [PATCH] first step --- .../s3manager/CacheDataSourceFactory.java | 41 +++++++++++++++++++ .../asgardius/page/s3manager/VideoPlayer.java | 8 ++++ 2 files changed, 49 insertions(+) create mode 100644 app/src/main/java/asgardius/page/s3manager/CacheDataSourceFactory.java diff --git a/app/src/main/java/asgardius/page/s3manager/CacheDataSourceFactory.java b/app/src/main/java/asgardius/page/s3manager/CacheDataSourceFactory.java new file mode 100644 index 0000000..e6aa575 --- /dev/null +++ b/app/src/main/java/asgardius/page/s3manager/CacheDataSourceFactory.java @@ -0,0 +1,41 @@ +package asgardius.page.s3manager; + +import android.content.Context; + +import com.google.android.exoplayer2.database.DatabaseProvider; +import com.google.android.exoplayer2.upstream.DataSource; +import com.google.android.exoplayer2.upstream.DefaultDataSource; +import com.google.android.exoplayer2.upstream.FileDataSource; +import com.google.android.exoplayer2.upstream.cache.CacheDataSink; +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 com.google.android.exoplayer2.util.Util; + +import java.io.File; + +public class CacheDataSourceFactory implements DataSource.Factory { + private final Context context; + private final DefaultDataSource.Factory defaultDatasourceFactory; + private final long maxFileSize, maxCacheSize; + //DatabaseProvider databaseProvider = ExoDatabaseProvider(this); + + public CacheDataSourceFactory(Context context, long maxCacheSize, long maxFileSize) { + super(); + this.context = context; + this.maxCacheSize = maxCacheSize; + this.maxFileSize = maxFileSize; + String userAgent = Util.getUserAgent(context, context.getString(R.string.app_name)); + //DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter(); + defaultDatasourceFactory = new DefaultDataSource.Factory(this.context); + } + + @Override + public DataSource createDataSource() { + LeastRecentlyUsedCacheEvictor evictor = new LeastRecentlyUsedCacheEvictor(maxCacheSize); + SimpleCache simpleCache = new SimpleCache(new File(context.getCacheDir(), "media"), evictor); + return new CacheDataSource(simpleCache, defaultDatasourceFactory.createDataSource(), + new FileDataSource(), new CacheDataSink(simpleCache, maxFileSize), + CacheDataSource.FLAG_BLOCK_ON_CACHE | CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR, null); + } +} \ No newline at end of file diff --git a/app/src/main/java/asgardius/page/s3manager/VideoPlayer.java b/app/src/main/java/asgardius/page/s3manager/VideoPlayer.java index 0c83639..5be7957 100644 --- a/app/src/main/java/asgardius/page/s3manager/VideoPlayer.java +++ b/app/src/main/java/asgardius/page/s3manager/VideoPlayer.java @@ -3,6 +3,7 @@ package asgardius.page.s3manager; import androidx.appcompat.app.AppCompatActivity; import android.content.Context; +import android.net.Uri; import android.net.wifi.WifiManager; import android.os.Bundle; import android.os.PowerManager; @@ -13,6 +14,9 @@ import com.google.android.exoplayer2.ExoPlayer; import com.google.android.exoplayer2.MediaItem; import com.google.android.exoplayer2.PlaybackException; import com.google.android.exoplayer2.Player; +import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory; +import com.google.android.exoplayer2.source.MediaSource; +import com.google.android.exoplayer2.source.ProgressiveMediaSource; import com.google.android.exoplayer2.ui.StyledPlayerView; import com.google.android.exoplayer2.upstream.HttpDataSource; @@ -43,6 +47,8 @@ public class VideoPlayer extends AppCompatActivity { playerView = findViewById(R.id.player_view); // creating a variable for exoplayer player = new ExoPlayer.Builder(this).build(); + //MediaSource audioSource = new ProgressiveMediaSource(Uri.parse("url"), + // new CacheDataSourceFactory(this, 100 * 1024 * 1024, 5 * 1024 * 1024), new DefaultExtractorsFactory(), null, null); // Attach player to the view. playerView.setPlayer(player); MediaItem mediaItem = MediaItem.fromUri(videoURL); @@ -50,6 +56,8 @@ public class VideoPlayer extends AppCompatActivity { // Set the media item to be played. player.setMediaItem(mediaItem); // Prepare the player. + player.setPlayWhenReady(true); + //player.setMediaSource(audioSource); player.prepare(); // Start the playback. player.play();