使用 MediaLibraryService 提供内容

媒体应用程序通常包含按层次结构组织的媒体项目集合。例如,专辑中的歌曲或播放列表中的电视剧集。此媒体项目的层次结构称为媒体库。

Examples of media content arranged in a hierarchy
图 1:构成媒体库的媒体项目层次结构示例。

MediaLibraryService 提供标准化的 API 用于提供和访问您的媒体库。例如,当您为 Android Auto 添加对媒体应用程序的支持时,这将非常有用,因为它为您的媒体库提供了自己的安全驾驶 UI。

构建 MediaLibraryService

实现 MediaLibraryService实现 MediaSessionService 类似,不同之处在于在 onGetSession() 方法中,您应该返回 MediaLibrarySession 而不是 MediaSession

Kotlin

class PlaybackService : MediaLibraryService() {
  var mediaLibrarySession: MediaLibrarySession? = null
  var callback: MediaLibrarySession.Callback = object : MediaLibrarySession.Callback {...}

  // If desired, validate the controller before returning the media library session
  override fun onGetSession(controllerInfo: MediaSession.ControllerInfo): MediaLibrarySession? =
    mediaLibrarySession

  // Create your player and media library session in the onCreate lifecycle event
  override fun onCreate() {
    super.onCreate()
    val player = ExoPlayer.Builder(this).build()
    mediaLibrarySession = MediaLibrarySession.Builder(this, player, callback).build()
  }

  // Remember to release the player and media library session in onDestroy
  override fun onDestroy() {
    mediaLibrarySession?.run { 
      player.release()
      release()
      mediaLibrarySession = null
    }
    super.onDestroy()
  }
}

Java

class PlaybackService extends MediaLibraryService {
  MediaLibrarySession mediaLibrarySession = null;
  MediaLibrarySession.Callback callback = new MediaLibrarySession.Callback() {...};

  @Override
  public MediaLibrarySession onGetSession(MediaSession.ControllerInfo controllerInfo) {
    // If desired, validate the controller before returning the media library session
    return mediaLibrarySession;
  }

  // Create your player and media library session in the onCreate lifecycle event
  @Override
  public void onCreate() {
    super.onCreate();
    ExoPlayer player = new ExoPlayer.Builder(this).build();
    mediaLibrarySession = new MediaLibrarySession.Builder(this, player, callback).build();
  }

  // Remember to release the player and media library session in onDestroy
  @Override
  public void onDestroy() {
    if (mediaLibrarySession != null) {
      mediaLibrarySession.getPlayer().release();
      mediaLibrarySession.release();
      mediaLibrarySession = null;
    }
    super.onDestroy();
  }
}

请记住在清单文件中声明您的 Service 和所需的权限

<service
    android:name=".PlaybackService"
    android:foregroundServiceType="mediaPlayback"
    android:exported="true">
    <intent-filter>
        <action android:name="androidx.media3.session.MediaSessionService"/>
    </intent-filter>
</service>

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<!-- For targetSdk 34+ -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />

使用 MediaLibrarySession

MediaLibraryService API 预期您的媒体库以树状格式构建,具有一个根节点和子节点,这些子节点可能是 可播放的 或进一步 可浏览的

MediaLibrarySession 扩展了 MediaSession API,以添加内容浏览 API。与 MediaSession 回调 相比,MediaLibrarySession 回调 添加了以下方法:

相关的回调方法将包含一个 LibraryParams 对象,其中包含有关客户端应用程序感兴趣的内容树类型的其他信号。