exoplayer的media2扩展

ExoPlayer的media2扩展简化了与AndroidX media2SessionAPI的集成,允许通过蓝牙、GoogleAssistant等控制播放。MediaSession核心类负责播放控制和元数据共享。通过SessionPlayerConnector和SessionCallbackBuilder,可以自定义ExoPlayer的行为,如使用自定义MediaItemConverter处理媒体项转换,利用ControlDispatcher控制命令调度,以及通过SessionCallback定制会话行为。此外,还能设置权限、处理播放列表编辑和自定义动作。要获取MediaSessionCompat.Token,可以使用MediaSessionUtil的相关方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 ,您需要为其提供SessionPlayerMediaSession.SessionCallback 。 ExoPlayer的新media2扩展通过提供以下功能使此操作变得容易:

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实例的行为。

Using custom MediaItemConverter and ControlDispatcher instances when building a SessionPlayerConnector 在构建SessionPlayerConnector时使用自定义MediaItemConverter和ControlDispatcher实例

媒体项目转换(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作为默认实现提供。 自定义实现可用于修改或临时禁用某些命令,例如,以防止在播放广告时进行搜索。 可以通过调用SessionPlayerConnectorsetControlDispatcher方法来设置自定义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构建回调时,可以使用各种设置器来自定义其行为。 这是设置三个自定义组件的示例。 这些组件将在下面更详细地描述。

Using SessionCallbackBuilder setters to customizing a MediaSession.SessionCallback 使用SessionCallbackBuilder设置器自定义MediaSession.SessionCallback

控制器权限 (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实现的示例,该实现仅接受来自同一应用程序的连接:

An AllowedCommandProvider that only accepts connections from the same application. 仅接受来自同一应用程序的连接的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以供其他应用程序显示。 例如:

A MediaItemProvider that converts a media ID to a UriMediaItem with metadata. 一个MediaItemProvider,它将媒体ID转换为带有元数据的UriMediaItem。

自定义动作 (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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值