Skip to content

Commit

Permalink
Issue googlecast#124 Fix Cast button visibility
Browse files Browse the repository at this point in the history
Add empty callback to MediaRouter to avoid losing cast button when
activity is paused
  • Loading branch information
mudar committed Jan 24, 2023
1 parent 99d3de4 commit b006ebc
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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<CastSession> {

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand All @@ -234,4 +268,7 @@ public void onOverlayDismissed() {
});
}
}

private final class MyCallback extends MediaRouter.Callback {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand All @@ -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<CastSession> {
override fun onSessionEnded(session: CastSession, error: Int) {
Expand Down Expand Up @@ -90,6 +98,7 @@ class VideoBrowserActivity : AppCompatActivity() {
}
}
mCastContext = CastContext.getSharedInstance(this,castExecutor).result
mediaRouter = MediaRouter.getInstance(this)
}

private fun setupActionBar() {
Expand Down Expand Up @@ -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(
Expand All @@ -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()
Expand All @@ -188,4 +216,4 @@ class VideoBrowserActivity : AppCompatActivity() {
companion object {
private const val TAG = "VideoBrowserActivity"
}
}
}

0 comments on commit b006ebc

Please sign in to comment.