The AndroidX media2 session API is the new version of Android’s MediaSession API. By integrating your application’s media player with this API, you can allow playback to be controlled via Bluetooth media control keys, Google Assistant, Android Auto, and other applications running on the user’s device.
AndroidX media2会话API是Android MediaSession API的新版本。 通过将应用程序的媒体播放器与此API集成,您可以允许通过蓝牙媒体控制键,Google Assistant,Android Auto和在用户设备上运行的其他应用程序控制播放。
The core class in the media2 session API is MediaSession
, which is responsible for exposing your application’s playback controls and media metadata to other processes.
media2会话API中的核心类是MediaSession
,它负责将应用程序的播放控件和媒体元数据公开给其他进程。
To instantiate a MediaSession
, you need to provide it with a SessionPlayer
and a MediaSession.SessionCallback
. ExoPlayer’s new media2 extension makes this easy by providing:
要实例化MediaSession
,您需要为其提供SessionPlayer
和MediaSession.SessionCallback
。 ExoPlayer的新media2扩展通过提供以下功能使此操作变得容易:
SessionPlayerConnector
: An implementation ofSessionPlayer
that wraps anExoPlayer
instance.SessionPlayerConnector
:包装一个ExoPlayer
实例的SessionPlayer
的实现。SessionCallbackBuilder
: BuildsMediaSession.SessionCallback
instances for use withSessionPlayerConnector
.SessionCallbackBuilder
:构建MediaSession.SessionCallback
实例以与SessionPlayerConnector
一起使用。
The code snippet below shows how the media2 extension makes it simple to integrate ExoPlayer with the media2 session API.
下面的代码段显示了media2扩展如何使将ExoPlayer与media2会话API集成变得简单。
自定义SessionPlayerConnector (Customizing a SessionPlayerConnector)
SessionPlayerConnector
is an implementation of the AndroidX’s media2 SessionPlayer
interface, which is the base interface for all media players that want to integrate with media2 APIs. SessionPlayerConnector
wraps an ExoPlayer
instance, and so acts as a translation layer between the ExoPlayer and media2 APIs.
SessionPlayerConnector
是AndroidX的media2 SessionPlayer
接口的实现,该接口是所有要与media2 API集成的媒体播放器的基本接口。 SessionPlayerConnector
包装一个ExoPlayer
实例,因此充当ExoPlayer和media2 API之间的转换层。
Within the SessionPlayerConnector
translation layer, a MediaItemConverter
is responsible for converting between the different media item representations used by the two APIs. Commands on the player are dispatched via a ControlDispatcher
. Custom implementations of these components can be used to modify the behavior of a SessionPlayerConnector
instance.
在SessionPlayerConnector
转换层中, MediaItemConverter
负责在两个API使用的不同媒体项表示之间进行转换。 通过ControlDispatcher
调度播放器上的命令。 这些组件的自定义实现可用于修改SessionPlayerConnector
实例的行为。
媒体项目转换(Media item conversion)
ExoPlayer and media2 have different representations of a media item. A MediaItemConverter
converts ExoPlayer MediaItem
instances to media2 MediaItem
instances, and vice-versa.
ExoPlayer和media2具有媒体项目的不同表示形式。 MediaItemConverter
将ExoPlayer MediaItem
实例转换为media2 MediaItem
实例,反之亦然。
DefaultMediaItemConverter
is provided as a default implementation of MediaItemConverter
. A common reason for providing a custom implementation is to inject additional MediaMetadata
into the media2 MediaItem
for sharing outside of the application. This can be done by extending DefaultMediaItemConverter
and overriding its getMetadata
method.
提供DefaultMediaItemConverter
作为MediaItemConverter
的默认实现。 提供自定义实现的常见原因是将额外的MediaMetadata
注入到media2 MediaItem
以便在应用程序外部共享。 这可以通过扩展DefaultMediaItemConverter
并覆盖其getMetadata
方法来完成。
控制调度 (Control dispatching)
ControlDispatcher
is responsible for dispatching commands to the player. DefaultControlDispatcher
is provided as a default implementation. Custom implementations can be used to modify or temporarily disable certain commands, for example to prevent seeking during playback of an advert. A custom ControlDispatcher
can be set on a SessionPlayerConnector
by calling its setControlDispatcher
method.
ControlDispatcher
负责将命令分配给播放器。 DefaultControlDispatcher
作为默认实现提供。 自定义实现可用于修改或临时禁用某些命令,例如,以防止在播放广告时进行搜索。 可以通过调用SessionPlayerConnector
的setControlDispatcher
方法来设置自定义ControlDispatcher
。
When using a custom ControlDispatcher
, it often makes sense to set the same instance on UI components in your application as well. ExoPlayer’s built in UI components support this by defining the same setControlDispatcher
method.
使用自定义ControlDispatcher
,通常也需要在应用程序的UI组件上设置相同的实例。 ExoPlayer的内置UI组件通过定义相同的setControlDispatcher
方法来支持此setControlDispatcher
。
自定义MediaSession.SessionCallback (Customizing a MediaSession.SessionCallback)
A MediaSession.SessionCallback
handles incoming commands from MediaController
instances that are interacting with the MediaSession. The ExoPlayer media2 extension provides SessionCallbackBuilder
for building instances suitable for use with SessionPlayerConnector
. When building a callback using SessionCallbackBuilder
, various setters can be used to customize its behavior. Here’s an example in which three custom components are set. The components are described in more detail below.
MediaSession.SessionCallback
处理与MediaSession交互的MediaController
实例的传入命令。 ExoPlayer media2扩展提供了SessionCallbackBuilder
用于构建适合与SessionPlayerConnector
一起使用的实例。 使用SessionCallbackBuilder
构建回调时,可以使用各种设置器来自定义其行为。 这是设置三个自定义组件的示例。 这些组件将在下面更详细地描述。
控制器权限 (Controller permissions)
The media2 MediaSession API supports finer grained permissions than the legacy MediaSession API. The API allows you to accept or reject connections from a controller, and also to allow or disallow individual commands. The allowed commands can be specified based on the caller, and can be changed dynamically.
与旧版MediaSession API相比,media2 MediaSession API支持更精细的权限。 该API允许您接受或拒绝来自控制器的连接,还可以允许或禁止单个命令。 可以根据调用方指定允许的命令,并且可以动态更改。
An AllowedCommandProvider
can be used to specify controller permissions. Here’s an example of an AllowedCommandProvider
implementation that only accepts connections from the same application:
可以使用AllowedCommandProvider
指定控制器权限。 这是一个AllowedCommandProvider
实现的示例,该实现仅接受来自同一应用程序的连接:
The media2 extension provides DefaultAllowedCommandProvider
as a default implementation of AllowedCommandProvider
. It behaves in a similar way to the legacy MediaSession API. It only accepts connections from trusted applications, which can call MediaSessionManager.getActiveSessions()
, in addition to your own extra trusted package names.
media2扩展提供DefaultAllowedCommandProvider
作为AllowedCommandProvider
的默认实现。 它的行为与旧版MediaSession API相似。 它只接受来自受信任的应用程序的连接,这些连接可以调用MediaSessionManager.getActiveSessions()
以及您自己的额外受信任的软件包名称。
处理播放列表编辑 (Handling playlist edits)
Media2 controllers can request that a new media item with a specified media ID be added to the player’s playlist. When this occurs, the session is expected to create the corresponding media2 MediaItem
and add it to the playlist. This is done by a MediaItemProvider
.
Media2控制器可以请求将具有指定媒体ID的新媒体项添加到播放器的播放列表中。 发生这种情况时,会话应创建相应的media2 MediaItem
并将其添加到播放列表中。 这是由MediaItemProvider
完成的。
A custom implementation may wish to add metadata into the MediaItem
for other applications to display. For example:
定制实现可能希望将元数据添加到MediaItem
以供其他应用程序显示。 例如:
自定义动作 (Custom actions)
Applications often want to provide additional custom actions to be displayed in a remote player UI, such as those of Android Audio and Wear OS.
应用程序通常想要提供其他自定义操作,以显示在远程播放器UI中,例如Android Audio和Wear OS的操作。
The CustomCommandProvider
interface can be implemented to define custom actions. The getCustomCommands
method should be implemented to return custom actions that should be advertised to media controllers. When a media controller sends a custom action, the onCustomCommands
method will be invoked to handle it.
可以实现CustomCommandProvider
接口来定义自定义动作。 应该实现getCustomCommands
方法以返回应通告给媒体控制器的自定义操作。 当媒体控制器发送自定义操作时,将调用onCustomCommands
方法进行处理。
获取MediaSessionCompat.Token (Obtaining a MediaSessionCompat.Token)
Your application may depend on APIs that require a MediaSessionCompat.Token
, such as PlayerNotificationManager
. To obtain a token for a media2 MediaSession, MediaSessionUtil.getSessionCompatToken
can be used:
您的应用程序可能取决于需要MediaSessionCompat.Token
API,例如PlayerNotificationManager
。 要获取media2 MediaSession的令牌,可以使用MediaSessionUtil.getSessionCompatToken
:
The ExoPlayer media2 extension makes it easy to integrate ExoPlayer with the media2 MediaSession API. It does all the heavy lifting for you, whilst still giving you full control over the behavior of all of the media actions defined by the API.
通过ExoPlayer media2扩展,可以轻松地将ExoPlayer与media2 MediaSession API集成在一起。 它为您完成了所有繁重的工作,同时仍使您可以完全控制API定义的所有媒体操作的行为。
We’re looking forward to seeing the media2 extension working in your application. We are happy to receive feedback and feature requests on our Github Issue tracker.
我们期待看到media2扩展在您的应用程序中正常工作。 我们很高兴在Github Issue跟踪器上收到反馈和功能请求。
翻译自: https://medium.com/google-exoplayer/the-media2-extension-for-exoplayer-d6b7d89b9063