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 22, 2023
1 parent 1a4f971 commit 1bb242e
Show file tree
Hide file tree
Showing 3 changed files with 77 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
37 changes: 35 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,37 +21,49 @@

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;

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;

private Player player;

private final IBinder mBinder = new LocalBinder();
private MediaBrowserConnector mediaBrowserConnector;
private final CompositeDisposable compositeDisposableLoadChildren = new CompositeDisposable();

/*//////////////////////////////////////////////////////////////////////////
// Service's LifeCycle
//////////////////////////////////////////////////////////////////////////*/

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

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

private void cleanup() {
Expand All @@ -144,6 +157,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 @@ -161,4 +177,21 @@ 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) {
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 1bb242e

Please sign in to comment.