mediasession

This commit is contained in:
Page Asgardius 2022-10-09 13:31:26 -07:00
parent f6165c6d6c
commit 405095a975
2 changed files with 19 additions and 5 deletions

View file

@ -36,7 +36,9 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.3.0' implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.4.0' implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4' implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'com.google.android.exoplayer:exoplayer:2.18.1' def exoplayer_version = "2.18.1"
implementation "com.google.android.exoplayer:exoplayer:$exoplayer_version"
implementation "com.google.android.exoplayer:extension-mediasession:$exoplayer_version"
implementation 'androidx.recyclerview:recyclerview:1.2.1' implementation 'androidx.recyclerview:recyclerview:1.2.1'
// BEGIN AWS DEPENDENCIES // BEGIN AWS DEPENDENCIES
def aws_version = "2.54.0" def aws_version = "2.54.0"

View file

@ -10,6 +10,7 @@ import android.net.wifi.WifiManager;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.PowerManager; import android.os.PowerManager;
import android.support.v4.media.session.MediaSessionCompat;
import android.view.View; import android.view.View;
import android.widget.Toast; import android.widget.Toast;
@ -20,6 +21,7 @@ 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.database.StandaloneDatabaseProvider; import com.google.android.exoplayer2.database.StandaloneDatabaseProvider;
import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector;
import com.google.android.exoplayer2.source.ProgressiveMediaSource; 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.DefaultHttpDataSource; import com.google.android.exoplayer2.upstream.DefaultHttpDataSource;
@ -51,11 +53,15 @@ public class VideoPlayer extends AppCompatActivity {
DefaultRenderersFactory renderersFactory; DefaultRenderersFactory renderersFactory;
ExoPlayer player; ExoPlayer player;
long videoPosition; long videoPosition;
MediaSessionCompat mediaSession;
MediaSessionConnector mediaSessionConnector;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_player); setContentView(R.layout.activity_video_player);
mediaSession = new MediaSessionCompat(this, getPackageName());
mediaSessionConnector = new MediaSessionConnector(mediaSession);
// create Wifi and wake locks // create Wifi and wake locks
mWifiLock = ((WifiManager) this.getApplicationContext().getSystemService(Context.WIFI_SERVICE)).createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, "S3Manager:wifi_lock"); mWifiLock = ((WifiManager) this.getApplicationContext().getSystemService(Context.WIFI_SERVICE)).createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, "S3Manager:wifi_lock");
powerManager = (PowerManager) getSystemService(POWER_SERVICE); powerManager = (PowerManager) getSystemService(POWER_SERVICE);
@ -74,6 +80,8 @@ 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).setLoadControl(loadControl).build(); player = new ExoPlayer.Builder(this).setLoadControl(loadControl).build();
mediaSessionConnector.setPlayer(player);
mediaSession.setActive(true);
evictor = new LeastRecentlyUsedCacheEvictor(maxCacheSize); evictor = new LeastRecentlyUsedCacheEvictor(maxCacheSize);
simpleCache = new SimpleCache( simpleCache = new SimpleCache(
new File(this.getCacheDir(), "media"), new File(this.getCacheDir(), "media"),
@ -198,9 +206,11 @@ public class VideoPlayer extends AppCompatActivity {
} }
public void onDestroy() { public void onDestroy() {
simpleCache.release(); mediaSessionConnector.setPlayer(null);
playerView.setPlayer(null); mediaSession.setActive(false);
player.release(); player.release();
playerView.setPlayer(null);
simpleCache.release();
super.onDestroy(); super.onDestroy();
} }
@ -209,9 +219,11 @@ public class VideoPlayer extends AppCompatActivity {
&& this.getPackageManager() && this.getPackageManager()
.hasSystemFeature( .hasSystemFeature(
PackageManager.FEATURE_PICTURE_IN_PICTURE)) { PackageManager.FEATURE_PICTURE_IN_PICTURE)) {
simpleCache.release(); mediaSessionConnector.setPlayer(null);
playerView.setPlayer(null); mediaSession.setActive(false);
player.release(); player.release();
playerView.setPlayer(null);
simpleCache.release();
} }
super.onStop(); super.onStop();
} }