From b006ebca4f195b09b8a14aba3fbc5abe7e0c8c65 Mon Sep 17 00:00:00 2001 From: Mudar Noufal Date: Tue, 24 Jan 2023 14:14:20 -0500 Subject: [PATCH] Issue #124 Fix Cast button visibility Add empty callback to MediaRouter to avoid losing cast button when activity is paused --- .../cast/refplayer/VideoBrowserActivity.java | 63 +++++++++++++++---- .../cast/refplayer/VideoBrowserActivity.kt | 56 ++++++++++++----- 2 files changed, 92 insertions(+), 27 deletions(-) diff --git a/app-java/src/main/java/com/google/sample/cast/refplayer/VideoBrowserActivity.java b/app-java/src/main/java/com/google/sample/cast/refplayer/VideoBrowserActivity.java index 2026817..68a473d 100644 --- a/app-java/src/main/java/com/google/sample/cast/refplayer/VideoBrowserActivity.java +++ b/app-java/src/main/java/com/google/sample/cast/refplayer/VideoBrowserActivity.java @@ -16,6 +16,22 @@ package com.google.sample.cast.refplayer; +import android.content.Intent; +import android.net.Uri; +import android.os.Bundle; +import android.os.Handler; +import android.util.Log; +import android.view.KeyEvent; +import android.view.Menu; +import android.view.MenuItem; + +import androidx.annotation.NonNull; +import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.Toolbar; +import androidx.mediarouter.media.MediaControlIntent; +import androidx.mediarouter.media.MediaRouteSelector; +import androidx.mediarouter.media.MediaRouter; + import com.google.android.gms.cast.framework.CastButtonFactory; import com.google.android.gms.cast.framework.CastContext; import com.google.android.gms.cast.framework.CastSession; @@ -26,19 +42,6 @@ import com.google.sample.cast.refplayer.queue.ui.QueueListViewActivity; import com.google.sample.cast.refplayer.settings.CastPreference; -import android.content.Intent; -import android.net.Uri; -import android.os.Bundle; -import android.os.Handler; -import androidx.annotation.NonNull; -import androidx.appcompat.app.AppCompatActivity; -import androidx.appcompat.widget.Toolbar; - -import android.util.Log; -import android.view.KeyEvent; -import android.view.Menu; -import android.view.MenuItem; - import java.util.concurrent.Executor; import java.util.concurrent.Executors; @@ -58,6 +61,9 @@ public class VideoBrowserActivity extends AppCompatActivity { private IntroductoryOverlay mIntroductoryOverlay; private CastStateListener mCastStateListener; private Executor localExecutor = Executors.newSingleThreadExecutor(); + private MediaRouter mRouter; + private MediaRouter.Callback mCallback = new MyCallback(); + private MediaRouteSelector mSelector; private class MySessionManagerListener implements SessionManagerListener { @@ -126,6 +132,12 @@ public void onCastStateChanged(int newState) { } }; mCastContext = CastContext.getSharedInstance(this,localExecutor).getResult(); + + mRouter = MediaRouter.getInstance(this); + mSelector = new MediaRouteSelector.Builder() + .addControlCategory(MediaControlIntent.CATEGORY_LIVE_AUDIO) + .addControlCategory(MediaControlIntent.CATEGORY_REMOTE_PLAYBACK) + .build(); } private void setupActionBar() { @@ -200,6 +212,20 @@ protected void onResume() { super.onResume(); } + @Override + public void onStart() { + super.onStart(); + + mRouter.addCallback(mSelector, mCallback, MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY); + } + + @Override + public void onStop() { + mRouter.addCallback(mSelector, mCallback, /* flags= */ 0); + + super.onStop(); + } + @Override protected void onPause() { mCastContext.removeCastStateListener(mCastStateListener); @@ -208,6 +234,14 @@ protected void onPause() { super.onPause(); } + @Override + public void onDestroy() { + // Remove the callback when the activity is destroyed. + mRouter.removeCallback(mCallback); + + super.onDestroy(); + } + private void showIntroductoryOverlay() { if (mIntroductoryOverlay != null) { mIntroductoryOverlay.remove(); @@ -234,4 +268,7 @@ public void onOverlayDismissed() { }); } } + + private final class MyCallback extends MediaRouter.Callback { + } } diff --git a/app-kotlin/src/main/kotlin/com/google/sample/cast/refplayer/VideoBrowserActivity.kt b/app-kotlin/src/main/kotlin/com/google/sample/cast/refplayer/VideoBrowserActivity.kt index af37a48..0067a44 100644 --- a/app-kotlin/src/main/kotlin/com/google/sample/cast/refplayer/VideoBrowserActivity.kt +++ b/app-kotlin/src/main/kotlin/com/google/sample/cast/refplayer/VideoBrowserActivity.kt @@ -15,29 +15,31 @@ */ package com.google.sample.cast.refplayer -import androidx.appcompat.app.AppCompatActivity -import com.google.android.gms.cast.framework.CastContext -import com.google.android.gms.cast.framework.SessionManagerListener -import com.google.android.gms.cast.framework.CastSession -import android.view.MenuItem -import com.google.android.gms.cast.framework.IntroductoryOverlay -import com.google.android.gms.cast.framework.CastStateListener -import android.os.Bundle -import com.google.android.gms.cast.framework.CastState -import android.view.View import android.content.Intent import android.net.Uri +import android.os.Bundle import android.os.Handler import android.os.Looper import android.util.Log -import android.view.Menu -import com.google.android.gms.cast.framework.CastButtonFactory import android.view.KeyEvent +import android.view.Menu +import android.view.MenuItem +import android.view.View +import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.widget.Toolbar +import androidx.mediarouter.media.MediaControlIntent +import androidx.mediarouter.media.MediaRouteSelector +import androidx.mediarouter.media.MediaRouter +import com.google.android.gms.cast.framework.CastButtonFactory +import com.google.android.gms.cast.framework.CastContext +import com.google.android.gms.cast.framework.CastSession +import com.google.android.gms.cast.framework.CastState +import com.google.android.gms.cast.framework.CastStateListener +import com.google.android.gms.cast.framework.IntroductoryOverlay +import com.google.android.gms.cast.framework.SessionManagerListener import com.google.sample.cast.refplayer.queue.ui.QueueListViewActivity import com.google.sample.cast.refplayer.settings.CastPreference import java.util.concurrent.Executor -import java.util.concurrent.ExecutorService import java.util.concurrent.Executors /** @@ -53,6 +55,12 @@ class VideoBrowserActivity : AppCompatActivity() { private var mIntroductoryOverlay: IntroductoryOverlay? = null private var mCastStateListener: CastStateListener? = null private val castExecutor: Executor = Executors.newSingleThreadExecutor(); + private var mediaRouter: MediaRouter? = null + private var mediaRouterCallback = object : MediaRouter.Callback() {} + private var mediaRouteSelector: MediaRouteSelector = MediaRouteSelector.Builder() + .addControlCategory(MediaControlIntent.CATEGORY_LIVE_AUDIO) + .addControlCategory(MediaControlIntent.CATEGORY_REMOTE_PLAYBACK) + .build() private inner class MySessionManagerListener : SessionManagerListener { override fun onSessionEnded(session: CastSession, error: Int) { @@ -90,6 +98,7 @@ class VideoBrowserActivity : AppCompatActivity() { } } mCastContext = CastContext.getSharedInstance(this,castExecutor).result + mediaRouter = MediaRouter.getInstance(this) } private fun setupActionBar() { @@ -158,6 +167,15 @@ class VideoBrowserActivity : AppCompatActivity() { super.onResume() } + override fun onStart() { + mediaRouter?.addCallback( + mediaRouteSelector, + mediaRouterCallback, + MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY + ) + super.onStart() + } + override fun onPause() { mCastContext!!.removeCastStateListener(mCastStateListener!!) mCastContext!!.sessionManager.removeSessionManagerListener( @@ -166,6 +184,16 @@ class VideoBrowserActivity : AppCompatActivity() { super.onPause() } + override fun onStop() { + mediaRouter?.addCallback(mediaRouteSelector, mediaRouterCallback, 0) + super.onStop() + } + + override fun onDestroy() { + mediaRouter?.removeCallback(mediaRouterCallback) + super.onDestroy() + } + private fun showIntroductoryOverlay() { if (mIntroductoryOverlay != null) { mIntroductoryOverlay!!.remove() @@ -188,4 +216,4 @@ class VideoBrowserActivity : AppCompatActivity() { companion object { private const val TAG = "VideoBrowserActivity" } -} \ No newline at end of file +}