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