Android cast devices (Available Routes) are not showing on MediaRouterDialog in android?

127 Views Asked by At

I want to cast a local video to android tv or smart tv and I'm using the MediaRouter API for this but routes are not discovering on MediaRouterDialog event a youtube or other casting apps discovering the devices on the same LAN network. I'm using the same network for discovering and follow the documentation but I think their's a mistake in code implementation. Here is my code implementation If anyone know this issue, please answer this, thanks

I'm using these permissions in menifest file

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.INTERNET" />

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMain2Binding.inflate(layoutInflater)
        setContentView(binding.root)
        mediaRouter = MediaRouter.getInstance(applicationContext)

        mSelector = MediaRouteSelector.Builder()
            .addControlCategory(MediaControlIntent.CATEGORY_REMOTE_PLAYBACK)
            .addControlCategory(MediaControlIntent.CATEGORY_LIVE_AUDIO)
            .addControlCategory(MediaControlIntent.CATEGORY_LIVE_VIDEO)
            .build()
        mediaRouter?.addCallback(
            mSelector!!, mediaRouterCallback,
            MediaRouter.CALLBACK_FLAG_PERFORM_ACTIVE_SCAN
        )
        binding.castButton.setOnClickListener {
            dialog = MediaRouteChooserDialog(this)
            dialog!!.routeSelector = mSelector as MediaRouteSelector
            dialog!!.show()
        }
    }

For callbacks

private val mediaRouterCallback = object : MediaRouter.Callback() {

        override fun onRouteSelected(
            router: MediaRouter,
            route: MediaRouter.RouteInfo,
            reason: Int
        ) {
            super.onRouteSelected(router, route, reason)
            Log.d(TAG, "onRouteSelected: route=$route")
            val name = route.name
            Toast.makeText(this@MainActivity2, name, Toast.LENGTH_SHORT).show()
        }

        override fun onRouteRemoved(router: MediaRouter, route: MediaRouter.RouteInfo) {
            super.onRouteRemoved(router, route)
            Log.d(TAG, "onRouteRemoved: route=$route")
            val name = route.name
            Toast.makeText(this@MainActivity2, "$name\nRoute Removed", Toast.LENGTH_SHORT).show()

        }
        override fun onRouteUnselected(
            router: MediaRouter,
            route: MediaRouter.RouteInfo,
            reason: Int
        ) {
            Log.d(TAG, "onRouteUnselected: route=$route")
            if (route.supportsControlCategory(MediaControlIntent.CATEGORY_REMOTE_PLAYBACK)) {

            }
        }
    }
1

There are 1 best solutions below

0
On

Try to change CALLBACK_FLAG_PERFORM_ACTIVE_SCAN to CALLBACK_FLAG_REQUEST_DISCOVERY like this

mediaRouter?.addCallback(
        mSelector!!, mediaRouterCallback,
        MediaRouter.CALLBACK_FLAG_PERFORM_ACTIVE_SCAN
    )

to

mediaRouter?.addCallback(
            mSelector!!, mediaRouterCallback,
            MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY
        )