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: #1758
  • Loading branch information
haggaie committed Dec 25, 2022
1 parent e07eaa9 commit 4a28129
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
5 changes: 4 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,14 @@

<service
android:name=".player.PlayerService"
android:exported="false"
android:exported="true"
android:foregroundServiceType="mediaPlayback">
<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
41 changes: 39 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,21 +21,28 @@

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 org.schabi.newpipe.player.mediasession.MediaSessionPlayerUi;
import org.schabi.newpipe.util.ThemeHelper;

import java.util.ArrayList;
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 @@ -50,6 +57,8 @@ public final class PlayerService extends Service {

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

if (DEBUG) {
Log.d(TAG, "onCreate() called");
}
Expand Down Expand Up @@ -133,6 +142,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 @@ -146,4 +158,29 @@ public Player getPlayer() {
return PlayerService.this.player;
}
}

// MediaBrowserServiceCompat methods

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

@Nullable
@Override
public 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);
}

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

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

result.sendResult(mediaItems);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public void initPlayer() {

mediaSession = new MediaSessionCompat(context, TAG);
mediaSession.setActive(true);
player.getService().setSessionToken(mediaSession.getSessionToken());

sessionConnector = new MediaSessionConnector(mediaSession);
sessionConnector.setQueueNavigator(new PlayQueueNavigator(mediaSession, player));
Expand Down

0 comments on commit 4a28129

Please sign in to comment.