3

I am trying to implement cast to tv feature using MediaRouteButton and exoPlayer. I have a strange issue with MediaRouteButton, I just followed this tutorial and implemented MediaRouteButton as a view not as part of the menu. Here what I have done till now, but after running the app, the MediaRouteButton is disabled.

This is layout:

<androidx.mediarouter.app.MediaRouteButton
    android:id="@+id/media_route_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:mediaRouteTypes="user"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintStart_toStartOf="parent"
     />

This is kotlin code inside activity:

class TestActivity : AppCompatActivity() {
    
    //var APP_ID = "4F8B3483"
    var mMediaRouteButton: MediaRouteButton? = null
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_test)

        mMediaRouteButton =  findViewById(R.id.media_route_button);

        CastContext.getSharedInstance(this);
        CastButtonFactory.setUpMediaRouteButton(this, mMediaRouteButton);
        
    }
}

Any suggestions?

Ichigo Kurosaki
  • 3,765
  • 8
  • 41
  • 56
Liridon Sadiku
  • 309
  • 3
  • 7

3 Answers3

1

You can try to use default media receiver id first:

  • CastMediaControlIntent.DEFAULT_MEDIA_RECEIVER_APPLICATION_ID, to see if your code works at all. If everything is ok, button will be visible and enabled.

If it's still not visible try to add:

  • castContext.addCastStateListener { state: Int -> } - and based on the callback show/hide MediaRouteButton manually.
nvr
  • 101
  • 3
  • 8
1

UPDATE July 7th 2023: this method is deprecated and does nothing

According to documentation:

the button is disabled and cannot be clicked unless setAlwaysVisible is called.

REF: https://developer.android.com/reference/androidx/mediarouter/app/MediaRouteButton

Zahra Jamshidi
  • 681
  • 6
  • 9
0

MediaRouteButton will be visible to you on the screen , when your device and casting device is connecting to the same Network .

Wahdat Jan
  • 3,988
  • 3
  • 21
  • 46
  • 1
    Hi Wahdat, thank you for your answer, they are already connected to the same network, activity is extending AppCompayActivity as it suggests in official site of android, almost all of the tutorials have the same approach but still cannot find a solution. MediaRouteButton is showing but not working( it is disabled). – Liridon Sadiku Jan 12 '21 at 14:08
  • I just run into the same issue. My problem was that I was behind a VPN. Disabling it at least once made the cast button enabled (even after putting the VPN back up). – Stephen Vinouze Aug 25 '22 at 15:30
  • In the youtube app, the menu is always visible and enabled. Then user could connect to a device. – Zahra Jamshidi Feb 27 '23 at 15:50