Skip to content

Commit

Permalink
Simple playback status and controls in Android Auto
Browse files Browse the repository at this point in the history
Expose a MediaBrowserService from within the existing PlayerService,
and use the existing MediaSession for Auto.

Empty media browser for now.

To test, one needs to enable "Unknown sources" in Android Auto's
developer settings.

Issue: TeamNewPipe#1758
  • Loading branch information
haggaie committed Feb 5, 2023
1 parent 8683ca7 commit 96ad0ff
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
<intent-filter>
<action android:name="android.media.browse.MediaBrowserService"/>
</intent-filter>
</service>

<activity
Expand Down
30 changes: 28 additions & 2 deletions app/src/main/java/org/schabi/newpipe/player/PlayerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,30 @@

import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.media.MediaBrowserCompat.MediaItem;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.media.MediaBrowserServiceCompat;

import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector;

import org.schabi.newpipe.player.mediabrowser.MediaBrowserConnector;
import org.schabi.newpipe.player.mediasession.MediaSessionPlayerUi;
import org.schabi.newpipe.util.ThemeHelper;

import java.util.List;

/**
* One service for all players.
*/
public final class PlayerService extends Service {
public final class PlayerService extends MediaBrowserServiceCompat {
private static final String TAG = PlayerService.class.getSimpleName();
private static final boolean DEBUG = Player.DEBUG;

Expand All @@ -52,6 +59,8 @@ public final class PlayerService extends Service {

@Override
public void onCreate() {
super.onCreate();

if (DEBUG) {
Log.d(TAG, "onCreate() called");
}
Expand Down Expand Up @@ -145,6 +154,9 @@ protected void attachBaseContext(final Context base) {

@Override
public IBinder onBind(final Intent intent) {
if (SERVICE_INTERFACE.equals(intent.getAction())) {
return super.onBind(intent);
}
return mBinder;
}

Expand All @@ -162,4 +174,18 @@ public Player getPlayer() {
return PlayerService.this.player;
}
}

// MediaBrowserServiceCompat methods
@Nullable
@Override
public BrowserRoot onGetRoot(@NonNull final String clientPackageName, final int clientUid,
@Nullable final Bundle rootHints) {
return mediaBrowserConnector.onGetRoot(clientPackageName, clientUid, rootHints);
}

@Override
public void onLoadChildren(@NonNull final String parentId,
@NonNull final Result<List<MediaItem>> result) {
mediaBrowserConnector.onLoadChildren(parentId, result);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
package org.schabi.newpipe.player.mediabrowser;

import android.os.Bundle;
import android.support.v4.media.MediaBrowserCompat;
import android.support.v4.media.MediaBrowserCompat.MediaItem;
import android.support.v4.media.session.MediaSessionCompat;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.media.MediaBrowserServiceCompat;
import androidx.media.MediaBrowserServiceCompat.Result;

import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector;

import org.schabi.newpipe.player.PlayerService;

import java.util.ArrayList;
import java.util.List;

public class MediaBrowserConnector {
private static final String TAG = MediaBrowserConnector.class.getSimpleName();

Expand All @@ -20,6 +30,7 @@ public MediaBrowserConnector(@NonNull final PlayerService playerService) {
mediaSession = new MediaSessionCompat(playerService, TAG);
sessionConnector = new MediaSessionConnector(mediaSession);
sessionConnector.setMetadataDeduplicationEnabled(true);
playerService.setSessionToken(mediaSession.getSessionToken());
}

public @NonNull MediaSessionConnector getSessionConnector() {
Expand All @@ -29,4 +40,26 @@ public MediaBrowserConnector(@NonNull final PlayerService playerService) {
public void release() {
mediaSession.release();
}

@NonNull
private static final String MY_MEDIA_ROOT_ID = "media_root_id";

@Nullable
public MediaBrowserServiceCompat.BrowserRoot onGetRoot(@NonNull final String clientPackageName,
final int clientUid,
@Nullable final Bundle rootHints) {
Log.d(TAG, String.format("MediaBrowserService.onGetRoot(%s, %s, %s)",
clientPackageName, clientUid, rootHints));

return new MediaBrowserServiceCompat.BrowserRoot(MY_MEDIA_ROOT_ID, null);
}

public void onLoadChildren(@NonNull final String parentId,
@NonNull final Result<List<MediaItem>> result) {
Log.d(TAG, String.format("MediaBrowserService.onLoadChildren(%s)", parentId));

final List<MediaBrowserCompat.MediaItem> mediaItems = new ArrayList<>();

result.sendResult(mediaItems);
}
}

0 comments on commit 96ad0ff

Please sign in to comment.