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 |
|
2. Sample of usage MediaServiceConnector
Create a media service destination by using the MediaServiceDestinationFactory method.
1 2 3 4 5 |
|
If a destination is available, you can create the media connector instance by adding the following code:
1 2 3 |
|
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 |
|
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 |
|
4. Usage
If you want to create an instance of a media facade, then add the following code:
1 |
|
Additionally, to create the object you can pass the notification listener class to use a custom implementation for the notification listener:
1 |
|
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 |
|
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 |
|
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 |
|
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 |
|
If you want to unsubscribe listener after you no longer need to observe sessions then add the following code:
1 |
|