Skip to content

Commit 4a28129

Browse files
committed
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
1 parent e07eaa9 commit 4a28129

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

app/src/main/AndroidManifest.xml

+4-1
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,14 @@
4747

4848
<service
4949
android:name=".player.PlayerService"
50-
android:exported="false"
50+
android:exported="true"
5151
android:foregroundServiceType="mediaPlayback">
5252
<intent-filter>
5353
<action android:name="android.intent.action.MEDIA_BUTTON" />
5454
</intent-filter>
55+
<intent-filter>
56+
<action android:name="android.media.browse.MediaBrowserService"/>
57+
</intent-filter>
5558
</service>
5659

5760
<activity

app/src/main/java/org/schabi/newpipe/player/PlayerService.java

+39-2
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,28 @@
2121

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

24-
import android.app.Service;
2524
import android.content.Context;
2625
import android.content.Intent;
2726
import android.os.Binder;
27+
import android.os.Bundle;
2828
import android.os.IBinder;
29+
import android.support.v4.media.MediaBrowserCompat.MediaItem;
2930
import android.util.Log;
3031

32+
import androidx.annotation.NonNull;
33+
import androidx.annotation.Nullable;
34+
import androidx.media.MediaBrowserServiceCompat;
35+
3136
import org.schabi.newpipe.player.mediasession.MediaSessionPlayerUi;
3237
import org.schabi.newpipe.util.ThemeHelper;
3338

39+
import java.util.ArrayList;
40+
import java.util.List;
3441

3542
/**
3643
* One service for all players.
3744
*/
38-
public final class PlayerService extends Service {
45+
public final class PlayerService extends MediaBrowserServiceCompat {
3946
private static final String TAG = PlayerService.class.getSimpleName();
4047
private static final boolean DEBUG = Player.DEBUG;
4148

@@ -50,6 +57,8 @@ public final class PlayerService extends Service {
5057

5158
@Override
5259
public void onCreate() {
60+
super.onCreate();
61+
5362
if (DEBUG) {
5463
Log.d(TAG, "onCreate() called");
5564
}
@@ -133,6 +142,9 @@ protected void attachBaseContext(final Context base) {
133142

134143
@Override
135144
public IBinder onBind(final Intent intent) {
145+
if (SERVICE_INTERFACE.equals(intent.getAction())) {
146+
return super.onBind(intent);
147+
}
136148
return mBinder;
137149
}
138150

@@ -146,4 +158,29 @@ public Player getPlayer() {
146158
return PlayerService.this.player;
147159
}
148160
}
161+
162+
// MediaBrowserServiceCompat methods
163+
164+
@NonNull
165+
private static final String MY_MEDIA_ROOT_ID = "media_root_id";
166+
167+
@Nullable
168+
@Override
169+
public BrowserRoot onGetRoot(@NonNull final String clientPackageName, final int clientUid,
170+
@Nullable final Bundle rootHints) {
171+
Log.d(TAG, String.format("MediaBrowserService.onGetRoot(%s, %s, %s)",
172+
clientPackageName, clientUid, rootHints));
173+
174+
return new MediaBrowserServiceCompat.BrowserRoot(MY_MEDIA_ROOT_ID, null);
175+
}
176+
177+
@Override
178+
public void onLoadChildren(@NonNull final String parentId,
179+
@NonNull final Result<List<MediaItem>> result) {
180+
Log.d(TAG, String.format("MediaBrowserService.onLoadChildren(%s)", parentId));
181+
182+
final List<MediaItem> mediaItems = new ArrayList<>();
183+
184+
result.sendResult(mediaItems);
185+
}
149186
}

app/src/main/java/org/schabi/newpipe/player/mediasession/MediaSessionPlayerUi.java

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public void initPlayer() {
4040

4141
mediaSession = new MediaSessionCompat(context, TAG);
4242
mediaSession.setActive(true);
43+
player.getService().setSessionToken(mediaSession.getSessionToken());
4344

4445
sessionConnector = new MediaSessionConnector(mediaSession);
4546
sessionConnector.setQueueNavigator(new PlayQueueNavigator(mediaSession, player));

0 commit comments

Comments
 (0)