使用 MediaLibraryService 提供内容

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

按层次结构排列的媒体内容示例
图 1:构成媒体库的媒体内容层次结构示例。

MediaLibraryService 提供了一个标准化 API,用于传送和访问您的媒体库。例如,当您向媒体应用添加对 Android Auto 的支持时,这会很有帮助,该应用为您的媒体库提供了自己的驾驶员安全界面。

构建 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 对象,其中包含有关客户端应用感兴趣的内容树类型的额外信号。