1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2025-01-08 16:30:34 +00:00

Simple playback status and controls in Android Auto

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
This commit is contained in:
Haggai Eran 2022-12-25 09:16:43 +02:00 committed by Siddhesh Naik
parent c73763d2c3
commit 34a4a27d46
3 changed files with 76 additions and 2 deletions

View File

@ -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

View File

@ -21,13 +21,18 @@ package org.schabi.newpipe.player;
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;
@ -37,11 +42,14 @@ import org.schabi.newpipe.util.ThemeHelper;
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;
@ -51,6 +59,7 @@ public final class PlayerService extends Service {
private MediaBrowserConnector mediaBrowserConnector;
private final CompositeDisposable compositeDisposableLoadChildren = new CompositeDisposable();
/*//////////////////////////////////////////////////////////////////////////
@ -59,6 +68,8 @@ public final class PlayerService extends Service {
@Override
public void onCreate() {
super.onCreate();
if (DEBUG) {
Log.d(TAG, "onCreate() called");
}
@ -158,6 +169,7 @@ public final class PlayerService extends Service {
mediaBrowserConnector.release();
mediaBrowserConnector = null;
}
compositeDisposableLoadChildren.clear();
}
private void cleanup() {
@ -179,6 +191,9 @@ public final class PlayerService extends Service {
@Override
public IBinder onBind(final Intent intent) {
if (SERVICE_INTERFACE.equals(intent.getAction())) {
return super.onBind(intent);
}
return mBinder;
}
@ -200,4 +215,21 @@ public final class PlayerService extends Service {
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);
}
}

View File

@ -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();
@ -20,6 +33,7 @@ public class MediaBrowserConnector {
mediaSession = new MediaSessionCompat(playerService, TAG);
sessionConnector = new MediaSessionConnector(mediaSession);
sessionConnector.setMetadataDeduplicationEnabled(true);
playerService.setSessionToken(mediaSession.getSessionToken());
}
public @NonNull MediaSessionConnector getSessionConnector() {
@ -29,4 +43,29 @@ public class MediaBrowserConnector {
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);
}
}