Android5.0 proposes a new mediasession concept for the player to interact with the controller, which replaces the previous remotecontrolclient and provides a more flexible client-side managed-end model, the following is its architecture diagram:
Its architecture is divided into 3 major parts:
1. Managed End (player):
The player needs to create a mediasession, which is similar to registering it in the system and telling the system that it can be controlled by the other control side.
2. Framework (intermediary):
The managed side is registered in the framework after the mediasession is created, and the framework records all the mediasession and provides query and status update Services to the control side.
3. Control side (playback status display and control):
The control end can be implemented in a variety of ways, can be implemented in another app, can also be implemented in the system UI, it mainly through the framework of the Mediasessionmanager to query the session in the system and according to their own needs to select the session to control , and can ask the system to notify itself when the session changes.
Implementation method:
1. Managed End (player):
The control side mainly needs to implement the media playback, this is no different from the previous, the next need to create a mediasession, the creation of the time need to set a unique session identifier, so that the control can be based on the identifier to determine the session of the control. Then you need to implement the session callback method (Sessioncallback), the callback method defines the media playback of the common operation, play, pause, next, previous, etc., When creating the session, you need to set the callback to the session, and implement the callback processing method, such as Onplay when the local player is started to play, onpause when the pause. At the same time, you need to set the status of the player through the session to the control side, Mediasession provides a special method setplaybackstate and Setmetadata to update the status of the local player and information to the control side.
2. Control side:
The control side needs to obtain the Mediasessionmanager through the system service first, then queries the session in the system, and determines the session instance to be controlled according to its own needs, for example, according to the identifier or the package name to confirm. Two things need to be done after the session is obtained, one is to get the Mediacontrol of the session, which controls the player to perform the specific operation, For example, call Mediacontrol's Play method will eventually be executed by the system player callback in the Onplay, and the second is to register their own callback to the session, The callback will be called when the player status changes to notify the console player of the status and data (media name, progress, etc.).
3. A simple managed end and control side example:
adjourned
Mediasession Architecture Analysis for Android 5.0 (L)