-
Notifications
You must be signed in to change notification settings - Fork 189
Open
Description
: MediaRouter.Callback() not getting called sometime ,discovery not working
class ScanCastingDevices(private val context: Context, private val myPreference: MyPreference) {
private var mRouter: MediaRouter? = null
private var mRoute: MediaRouter.RouteInfo? = null
private var castDevice: CastDevice? = null
var deviceInfo:TvDeviceInfo?=null
// Callback for media router
private val mediaRouterCallback = object : MediaRouter.Callback() {
override fun onRouteAdded(router: MediaRouter, route: MediaRouter.RouteInfo) {
Log.e("CastMediaRouter", "Route Added: $route")
castDevice = CastDevice.getFromBundle(route.extras)
if ((removeHyphens(deviceInfo?.serviceId.toString()) == castDevice?.deviceId) || (deviceInfo?.ipAddress?.equals(
castDevice?.inetAddress?.hostAddress
)) == true
) {
myPreference.setCastingDeviceInfo(CAST_DEVICE_INFO, castDevice)
Log.e("stored device info",myPreference.getCastingDeviceInfo(CAST_DEVICE_INFO)?.inetAddress?.hostAddress.orEmpty())
stopDiscovery()
}
}
override fun onRouteRemoved(router: MediaRouter, route: MediaRouter.RouteInfo) {
Log.e("CastMediaRouter", "onRouteRemoved$route : ")
castDevice = CastDevice.getFromBundle(route.extras)
}
}
// Initialize MediaRouter
fun initialize() {
deviceInfo= Utils.getConnectedDeviceInfo(myPreference)
Log.e("CastMediaRouter_ConnectedDeviceInfo",deviceInfo.toString())
Log.e("CastMediaRouter", "initialize")
mRouter = MediaRouter.getInstance(context)
Log.e("CastMediaRouter", "initialize_startDiscovery : ")
startDiscovery()
}
// Start discovery for Cast devices
private fun startDiscovery() {
Log.e("CastMediaRouter", "startDiscovery : ")
val selector = MediaRouteSelector.Builder()
.addControlCategory(CastMediaControlIntent.categoryForCast(CastMediaControlIntent.DEFAULT_MEDIA_RECEIVER_APPLICATION_ID))
.build()
mRouter?.addCallback(selector, mediaRouterCallback, MediaRouter.CALLBACK_FLAG_FORCE_DISCOVERY)
}
fun stopDiscovery(){
Log.e("CastMediaRouter", "stopDiscovery : ")
mRouter?.removeCallback(mediaRouterCallback)
}
}
Metadata
Metadata
Assignees
Labels
No labels