Skip to content

Media3 background player MediaItem nuances #125

Open
@Manuiq

Description

@Manuiq

I was amazed by the simplicity of the Media3 integration so I spent an evening trying to create a simple project with it . My setup was a MediaSessionService (with MediaSession and ExoPlayer) + Activity (that controls playback via MediaBrowser).

  1. I run into a small hiccup with this part:
val player = ExoPlayer.Builder(context).build()
val mediaSession = MediaSession.Builder(context, player)
   .build()

I assumed this setup was enough at least for the case of adding MediaItems via MediaBrowser from your own Activity via
mediaBroser.addMediaItems(items) for them to be passed on to the player. But turned out there it's not that easy and you need to override MediaSession.Callback interface's fun onAddMediaItems() as there is a really important comment in the source code on it:

"Called when a controller requested to add new media items to the playlist via one of the Player.addMediaItem(s) or Player.setMediaItem(s) methods.
Note that the requested media items don't have a MediaItem.LocalConfiguration (for example, a URI) and need to be updated to make them playable by the underlying Player. Typically, this implementation should be able to identify the correct item by its MediaItem.mediaId and/or the MediaItem.requestMetadata."

So turns out you can't just pass the items to player because the MediaItem is not serialized in full across processes which seems reasonable when you look at the LocalConfiguration class structure and all the difficulties with it's bundling/unbundling but it's not mentioned in any way anywhere else in the doc. Maybe this is all trivial for anyone working with previous media libraries but for a newbie in media playback it wasn't that obvious. So maybe addressing such points in the developer docs/codelabs can help?

  1. Also in the demos-session sample PlaybackService is described as MediaSessionService in the Manifest
    <service
        android:name=".PlaybackService"
        android:foregroundServiceType="mediaPlayback"
        android:exported="true">
      <intent-filter>
        <action android:name="androidx.media3.session.MediaSessionService"/>
        <action android:name="android.media.browse.MediaBrowserService"/>
      </intent-filter>
    </service>

but extends MediaLibraryService in the actual code
class PlaybackService : MediaLibraryService() {
which is a bit confusing also. Do they have the same handling intent-filter wise?

Still, the library is great and I admire the level of quality in your 1.0.0 release.

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions