Skip to content

Media Connector

What is Media Connector?

Media Connector is a component that allows you to get a seamless connection to all your media sources.

With Media Connector you can avoid all the burden for defining and maintaining connections to media sources.

How to implement Media Connector?

The following sections cover how you can get started using Media Connector. You can easily integrate the Media Service Connector, by following these steps.

1. Dependencies

Open the build.gradle file for your project and add the following dependency:

1
2
3
4
5
dependencies {
    // check out the latest version from artifactory    
    api "com.telenav.sdk.vivid:android-sdk-media-connector:${version}" 

 }

2. Sample of usage MediaServiceConnector

Create a media service destination by using the MediaServiceDestinationFactory method.

1
2
3
4
5
val destination = MediaServiceDestinationFactory.create(
        context,
        name,
        serviceInfo
)

If a destination is available, you can create the media connector instance by adding the following code:

1
2
3
if (destination.isAvailable()) {
val connector = MediaServiceConnector(destination)   
}

After calling the attach method on the connector, the response consists of strongly typed connection states which provide a definite set of values that captures the connection lifecycle of a MediaBrowserService.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
connector.attach(rootHints = null) { connectionState ->
        when (connectionState) {
            is ConnectionState.Prepared -> connectionState.browser.connect() // Performs the connection to the media browser, once the connection is prepared
            is ConnectionState.Connected -> onConnected( // The connection is established, the media browser and the media center can be used
                    connectionState.browser,
                    connectionState.controller
            )
            is ConnectionState.Suspended -> handleOnSuspended(connectionState.browser) // The service was disconnected
            is ConnectionState.Error -> handleOnError(connectionState.error) // An error occurred while connecting to the service
        }
    }

3. Media Session Handling

MediaSessionFacade exposes an efficient interface for interacting with MediaSession related concerns.

Notification Listener

The Notification Listener is a service that receives calls from the system when new notifications are posted or removed, or their ranking changed.

Interacting with media sources by using notification listener permission is a common practice for apps that are not explicitly whitelisted by a media source. Media connector provides a quick mechanism for achieving that.

First step is to formally declare the NotificationListener service, as shown below:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<application>
...
        <service
            android:name="com.telenav.sdk.vivid.media.connector.session.NotificationListener"
            android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">

            <intent-filter>
                <action android:name="android.service.notification.NotificationListenerService" />
            </intent-filter>
        </service>
...
</application>

4. Usage

If you want to create an instance of a media facade, then add the following code:

1
val mediaSessionFacade = MediaSessionFacade.Factory.create(applicationContext)

Additionally, to create the object you can pass the notification listener class to use a custom implementation for the notification listener:

1
val mediaSessionFacade = MediaSessionFacade.Factory.create(applicationContext, CustomNotificationListenerService::class.java)

Info

Before being able to subscribe and get the active media sessions, make sure to check if the notification access is granted.

To verify if the notification listener permission is granted, the code below can be used:

1
2
if(mediaSessionFacade.isNotificationListenerAccessGranted()){
}

Info

If notification access is not granted, you can't use getActiveSessions. Calling [add/remove] OnActiveSessionsChangedListener method will throw an exception.

If you want to get the active sessions, then can call the getActiveSessions() method from the media session facade object:

1
 val activeSessions = mediaSessionFacade.getActiveSessions()

If you want to be up to date with the changes regarding the active session then you need to create an instance of session listener as shown below:

1
2
3
val sessionListener = MediaSessionManager.OnActiveSessionsChangedListener { controllers ->
        TODO("Not yet implemented")
    }

Subscribe / Unsubscribe

Once the listener is created you can perform the following actions on it.

If you want to subscribe the listener to observe active sessions then add the following code:

1
 mediaSessionFacade.addOnActiveSessionsChangedListener(sessionListener)

If you want to unsubscribe listener after you no longer need to observe sessions then add the following code:

1
mediaSessionFacade.removeOnActiveSessionsChangedListener(sessionListener)