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 May 29, 2024
1 parent 4ad51ef commit 6aa7ad7
Show file tree
Hide file tree
Showing 3 changed files with 76 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 @@ -64,6 +64,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
36 changes: 34 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,13 +21,18 @@

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;
Expand All @@ -37,11 +42,14 @@

import java.lang.ref.WeakReference;

import java.util.List;

import io.reactivex.rxjava3.disposables.CompositeDisposable;

/**
* 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 @@ -51,6 +59,7 @@ public final class PlayerService extends Service {


private MediaBrowserConnector mediaBrowserConnector;
private final CompositeDisposable compositeDisposableLoadChildren = new CompositeDisposable();


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

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

if (DEBUG) {
Log.d(TAG, "onCreate() called");
}
Expand Down Expand Up @@ -158,6 +169,7 @@ public void onDestroy() {
mediaBrowserConnector.release();
mediaBrowserConnector = null;
}
compositeDisposableLoadChildren.clear();
}

private void cleanup() {
Expand All @@ -179,6 +191,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 @@ -200,4 +215,21 @@ public Player getPlayer() {
return playerService.get().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) {
result.detach();
final var disposable = mediaBrowserConnector.onLoadChildren(parentId)
.subscribe(result::sendResult);
compositeDisposableLoadChildren.add(disposable);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
package org.schabi.newpipe.player.mediabrowser;

import static org.schabi.newpipe.MainActivity.DEBUG;

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 com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector;

import org.schabi.newpipe.player.PlayerService;

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

import io.reactivex.rxjava3.core.Single;

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

Expand All @@ -20,6 +33,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 +43,29 @@ 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) {
if (DEBUG) {
Log.d(TAG, String.format("MediaBrowserService.onGetRoot(%s, %s, %s)",
clientPackageName, clientUid, rootHints));
}

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

public Single<List<MediaItem>> onLoadChildren(@NonNull final String parentId) {
if (DEBUG) {
Log.d(TAG, String.format("MediaBrowserService.onLoadChildren(%s)", parentId));
}

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

return Single.just(mediaItems);
}
}

0 comments on commit 6aa7ad7

Please sign in to comment.