One: Framework introduction

Audio and video make up the player and interface

A multimedia application that plays audio or video usually consists of two parts:

  • Player for absorbing digital media and presenting it as video and/or audio
  • Interface, with transport controls for running the player and (optionally) displaying the player status

Usually we write video player can use mediaPalyer, ExoPlayer, etc., UI we implement.

During the development process, we will implement a set of interfaces or callbacks to handle the direct communication between Ui and Player, which is also ok. However, if we add other Ui controls or other app connections, it will be troublesome.

Therefore, Google launched MediaSession framework, which can well decouple UI and player, as well as a unified interface, and can support control and connection of multiple devices to a greater extent.

MediaSession framework

While the interfaces and player apis may differ, the nature of the interaction between the two parts is essentially the same for all media player applications. The Android framework defines two classes (media sessions and media Controllers) that provide a complete structure for building media player applications.

Media sessions and media controllers communicate with each other using predefined callbacks corresponding to standard player actions (play, pause, stop, and so on) and extensible custom calls to define special behavior unique to the application.

MediaSession MediaSession

The media session is responsible for all communication with the player. It hides the player API from the rest of the application. The system can only call the player from the media session that controls the player.

MediaSession maintains a representation of the player state (play/pause) and information about what is being played. A session can receive callbacks from one or more media controllers. This way, your player is controlled by the app’s interface and companion devices running Wear OS and Android Auto. The logic in response to callbacks must be consistent. Regardless of which client application initiates the callback, the response to the MediaSession callback should be the same.

MediaControl Media controller

MediaControl isolates your interface. Your interface code communicates only with MediaControl, not the player itself.

MediaControl translates the transport control operation into a callback to MediaSession. It also receives a callback from the media session whenever the state of the session changes. This provides a mechanism for automatically updating the association interface. The media controller can only be connected to one media session at a time.

When you use media controllers and media sessions, you can deploy different interfaces and/or players at run time. You can individually change the look and/or performance of the application depending on the capabilities of the device on which it is running.

The architecture and differences between music app and video app

Audio app

UI + Player, which can play in the background and then operate other apps, so compared to video app, there are many MediaBrowser and MediaBrowserService

Video app

UI + Player, which cannot be played in the background, must be paused or quit, so it can be the completion of a single Activity. The screen displaying the video is part of the Activity, as shown below

advantages

Advantages: The use of a set of interfaces, reduce the tedious process and service communication, etc., to achieve the unified call of multiple devices or UI, its code readability, structure coupling degree control is very good.

AutoMotive MediaSession what is the difference between audio and video

Official: The construction of vehicle media applications can know that the MediaSession in cars remains unchanged, mainly supporting Auto, etc

This guide assumes that you already have a media application that can play audio on your phone and that your media application follows the Android Media Application architecture.

This guide describes the MediaBrowserService and MediaSession components required for your application to run on Android Auto or Android Automotive OS. After setting up the core media infrastructure, you can add support for Android Auto and Android Automotive OS to your media application.

The official Demo

  • UAMP – Universal music player uses ExoPlayer to play local audio.
  • ExoPlayer Demo App – The official code base includes a demo app that demonstrates many of the advanced features of the library.
  • Control the media through MediaSession

Two: the use of API

Detailed API flow

MediaBrowser

Media browser, used to connect MediaBrowserService to subscription data.

MediaBrowserService

Browser Service, providing onGetRoot (client connection request) and onLoadChildren (called when media browser sends data subscription to Service),

MediaBrowserService also acts as a container for media players (such as MediaPlayer, ExoPlayer, etc.) and MediaSession

PlaybackStateCompat is used to transmit the current play status, including pause, play, and play progress

MediaMetadataCompat An Item used to store music information

MediaSeesionCompat, MediaControllerCompat is an upgrade to MediaSeesion in the media-Compat library.

MusicDemo address

Use Kotlin, and reference the official and open source library code, write a music playback demo, as follows: github.com/TJYOYO/Medi…

The difference of MediaSession in video app

Since the background service of MediaBrowserService is not needed in the video, MediaSession and MediaController are connected in the video app. See the following figure. MediaController provides two ways to connect to MediaSession.

Three: more device control

Testing:

1: Other media apps

By connecting MusicDemo’s Service with MediaBrowser and MediaControl, you can remotely control music playback in MusicDemo. For example, only UI MediaBrowser and MediaControl can be connected to the official UAMP to support operations.

2: Media control provided by ADB Shell

  • adb shell media dispatch pause
  • adb shell media dispatch play
  • adb shell media dispatch play-pause
  • adb shell media dispatch fast-forward
  • adb shell media dispatch rewind

3: Support the control of physical keys -MediaSession callback method onMediaButtonEvent

  • adb shell input keyevent 87 // next
  • adb shell input keyevent 88 // previous
  • adb shell input keyevent 126 // play
  • adb shell input keyevent 127 // pause

4: Google Assistant

Commands like “pause,” “continue,” and “next,” so that voice functions don’t need to be implemented on every audio screen.

The next main introduction: MediaSession framework source analysis

Reference:

Official: Overview of Media Application Architecture

Android media playback framework MediaSession analysis and practice more detailed introduction, suitable for practice

Github.com/yinxuming/M… Demo, suitable for practice

www.codenong.com/cs106448963… The API in detail

MediaSession framework introduction suitable for the above demo exercise to see again

github.com/jrfeng/snow package perfect, suitable for use

Github.com/android/uam… Official, suitable for in-depth