From 405095a975631408534aa9ca5c81883ecbd8657a Mon Sep 17 00:00:00 2001 From: Page Asgardius Date: Sun, 9 Oct 2022 13:31:26 -0700 Subject: [PATCH] mediasession --- app/build.gradle | 4 +++- .../asgardius/page/s3manager/VideoPlayer.java | 20 +++++++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 36c17c4..a5a2a33 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -36,7 +36,9 @@ dependencies { implementation 'androidx.appcompat:appcompat:1.3.0' implementation 'com.google.android.material:material:1.4.0' 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' // BEGIN AWS DEPENDENCIES def aws_version = "2.54.0" diff --git a/app/src/main/java/asgardius/page/s3manager/VideoPlayer.java b/app/src/main/java/asgardius/page/s3manager/VideoPlayer.java index fed010d..cf020de 100644 --- a/app/src/main/java/asgardius/page/s3manager/VideoPlayer.java +++ b/app/src/main/java/asgardius/page/s3manager/VideoPlayer.java @@ -10,6 +10,7 @@ import android.net.wifi.WifiManager; import android.os.Build; import android.os.Bundle; import android.os.PowerManager; +import android.support.v4.media.session.MediaSessionCompat; import android.view.View; 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.Player; 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.ui.StyledPlayerView; import com.google.android.exoplayer2.upstream.DefaultHttpDataSource; @@ -51,11 +53,15 @@ public class VideoPlayer extends AppCompatActivity { DefaultRenderersFactory renderersFactory; ExoPlayer player; long videoPosition; + MediaSessionCompat mediaSession; + MediaSessionConnector mediaSessionConnector; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_video_player); + mediaSession = new MediaSessionCompat(this, getPackageName()); + mediaSessionConnector = new MediaSessionConnector(mediaSession); // create Wifi and wake locks mWifiLock = ((WifiManager) this.getApplicationContext().getSystemService(Context.WIFI_SERVICE)).createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, "S3Manager:wifi_lock"); powerManager = (PowerManager) getSystemService(POWER_SERVICE); @@ -74,6 +80,8 @@ public class VideoPlayer extends AppCompatActivity { playerView = findViewById(R.id.player_view); // creating a variable for exoplayer player = new ExoPlayer.Builder(this).setLoadControl(loadControl).build(); + mediaSessionConnector.setPlayer(player); + mediaSession.setActive(true); evictor = new LeastRecentlyUsedCacheEvictor(maxCacheSize); simpleCache = new SimpleCache( new File(this.getCacheDir(), "media"), @@ -198,9 +206,11 @@ public class VideoPlayer extends AppCompatActivity { } public void onDestroy() { - simpleCache.release(); - playerView.setPlayer(null); + mediaSessionConnector.setPlayer(null); + mediaSession.setActive(false); player.release(); + playerView.setPlayer(null); + simpleCache.release(); super.onDestroy(); } @@ -209,9 +219,11 @@ public class VideoPlayer extends AppCompatActivity { && this.getPackageManager() .hasSystemFeature( PackageManager.FEATURE_PICTURE_IN_PICTURE)) { - simpleCache.release(); - playerView.setPlayer(null); + mediaSessionConnector.setPlayer(null); + mediaSession.setActive(false); player.release(); + playerView.setPlayer(null); + simpleCache.release(); } super.onStop(); }