Merge pull request 'mediacache' (#10) from mediacache into main

Reviewed-on: https://patrice.asgardius.company/gitea/asgardius/s3manager/pulls/10
This commit is contained in:
Page Asgardius 2022-10-04 20:21:08 -07:00
commit 5cf91a5d7d
3 changed files with 68 additions and 6 deletions

View file

@ -9,8 +9,8 @@ android {
applicationId "asgardius.page.s3manager" applicationId "asgardius.page.s3manager"
minSdk 24 minSdk 24
targetSdk 33 targetSdk 33
versionCode 19 versionCode 20
versionName "0.1.17" versionName "0.1.18"
setProperty("archivesBaseName", "s3-manager-$versionName") setProperty("archivesBaseName", "s3-manager-$versionName")
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

View file

@ -0,0 +1,43 @@
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;
SimpleCache simpleCache;
//DatabaseProvider databaseProvider = ExoDatabaseProvider(this);
public CacheDataSourceFactory(Context context, SimpleCache simpleCache, long maxFileSize) {
super();
this.context = context;
this.simpleCache = simpleCache;
//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 = 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);
}
}

View file

@ -3,6 +3,7 @@ package asgardius.page.s3manager;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import android.content.Context; import android.content.Context;
import android.net.Uri;
import android.net.wifi.WifiManager; import android.net.wifi.WifiManager;
import android.os.Bundle; import android.os.Bundle;
import android.os.PowerManager; import android.os.PowerManager;
@ -13,8 +14,15 @@ import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.MediaItem; import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.PlaybackException; import com.google.android.exoplayer2.PlaybackException;
import com.google.android.exoplayer2.Player; 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.ui.StyledPlayerView;
import com.google.android.exoplayer2.upstream.HttpDataSource; import com.google.android.exoplayer2.upstream.HttpDataSource;
import com.google.android.exoplayer2.upstream.cache.LeastRecentlyUsedCacheEvictor;
import com.google.android.exoplayer2.upstream.cache.SimpleCache;
import java.io.File;
/** /**
* An example full-screen activity that shows and hides the system UI (i.e. * An example full-screen activity that shows and hides the system UI (i.e.
@ -27,6 +35,8 @@ public class VideoPlayer extends AppCompatActivity {
private WifiManager.WifiLock mWifiLock; private WifiManager.WifiLock mWifiLock;
private PowerManager.WakeLock mWakeLock; private PowerManager.WakeLock mWakeLock;
private PowerManager powerManager; private PowerManager powerManager;
private long maxCacheSize = 100 * 1024 * 1024;
SimpleCache simpleCache;
ExoPlayer player; ExoPlayer player;
@ -43,13 +53,21 @@ public class VideoPlayer extends AppCompatActivity {
playerView = findViewById(R.id.player_view); playerView = findViewById(R.id.player_view);
// creating a variable for exoplayer // creating a variable for exoplayer
player = new ExoPlayer.Builder(this).build(); player = new ExoPlayer.Builder(this).build();
LeastRecentlyUsedCacheEvictor evictor = new LeastRecentlyUsedCacheEvictor(maxCacheSize);
simpleCache = new SimpleCache(new File(this.getCacheDir(), "media"), evictor);
MediaSource mediaSource = new ProgressiveMediaSource.Factory(new CacheDataSourceFactory(this, simpleCache, 512 * 1024 * 1024))
.createMediaSource(MediaItem.fromUri(Uri.parse(videoURL)));
//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. // Attach player to the view.
playerView.setPlayer(player); playerView.setPlayer(player);
MediaItem mediaItem = MediaItem.fromUri(videoURL); //MediaItem mediaItem = MediaItem.fromUri(videoURL);
// Set the media item to be played. // Set the media item to be played.
player.setMediaItem(mediaItem); //player.setMediaItem(mediaItem);
// Prepare the player. // Prepare the player.
player.setPlayWhenReady(true);
player.setMediaSource(mediaSource);
player.prepare(); player.prepare();
// Start the playback. // Start the playback.
player.play(); player.play();
@ -138,13 +156,14 @@ public class VideoPlayer extends AppCompatActivity {
} }
public void onDestroy() { public void onDestroy() {
simpleCache.release();
player.release(); player.release();
super.onDestroy(); super.onDestroy();
} }
public void onBackPressed() { /*public void onBackPressed() {
player.release(); player.release();
finish(); finish();
} }*/
} }