Search code examples
c#android.netandroid-jetpackexoplayer

Jetpack Media3 in .Net


I've been writing a neat little music player for my phone using .Net Maui. So far - so good. Everything I've set out to get running has gone just fine, I'm just having a little trouble getting exactly what I wanted out of the ExoPlayer Library, so I though I'd ask.

At the moment the app's using ExoPlayer2, which is fine (other than all the Obsolete warnings). I'm wondering if anyone's been able to integrate some of the Jetpack Media3 libraries into .Net with any success.

All I really want is for my app to behave the same way as a lot of other music players. I want my media player notification to be docked in the quick settings, I want it to respond to Resume Playback events sent by the system, I want to be able to customize the notification thoroughly, and I want other apps to be able to read info from the Player to display in cars and on watches and stuff. If all that's possible with existing solutions on the older ExoPlayers in .net, I haven't had much luck finding any resources, which is why I'm now checking out Media3.

Kind Regards, Oli


Solution

  • If you're stumbling upon this with the same issues, just know it's probably because you're implementing things wrong.

    I managed to get what I wanted by starting my Foreground Service with a notification, settings its style with a MediaSession SessionToken. Here's how I've done it in my OnStartCommand for the Session itself.

    public override StartCommandResult OnStartCommand(Intent? intent, [GeneratedEnum] StartCommandFlags flags, int startId)
    {
        var channelName = "PortaJel";
        var channelDescription = "Notification Channel for the PortaJel Music Streaming App for Jellyfin";
        var channel = new NotificationChannel(channedId, channelName, NotificationImportance.Max)
        {
            Description = channelDescription,
        };
    
        Context context = Microsoft.Maui.ApplicationModel.Platform.AppContext;
    
        mediaSession = new MediaSession(context, channedId);
        mediaSessionCallback = new MediaSessionCallback();
        // Define callback functions here
    
        mediaSession.SetFlags(MediaSessionFlags.HandlesMediaButtons | MediaSessionFlags.HandlesTransportControls);
        mediaSession.SetCallback(mediaSessionCallback);
    
        Notification notification = new Notification.Builder(context, channel.Id)
             .SetChannelId(channel.Id)
             .SetSmallIcon(Resource.Drawable.ic_mtrl_checked_circle)
             .SetContentTitle("Track title")
             .SetContentText("Artist - Album")
             .SetStyle(new Notification.MediaStyle().SetMediaSession(mediaSession.SessionToken))
             .Build();
    
        StartForeground(SERVICE_RUNNING_NOTIFICATION_ID, notification);
        return base.OnStartCommand(intent, flags, startId);
    }
    

    The notificaiton will style itself correctly, and if you add function callbacks (like I haven't yet, see the line where it says 'Define callback functions here), it should work fine.

    Thanks again anyway!