Skip to content

Commit 68cf3c9

Browse files
Implement MediaSessionCompat.Callback for true android wear support
1 parent 6f1c788 commit 68cf3c9

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

android/app/src/main/java/com/marshallofsound/gpmdp/remote/InternalMediaService.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
import android.graphics.Bitmap;
77
import android.graphics.BitmapFactory;
88
import android.media.AudioManager;
9-
import android.os.Bundle;
10-
import android.os.ResultReceiver;
119
import android.support.v4.app.NotificationManagerCompat;
1210
import android.support.v4.media.MediaMetadataCompat;
1311
import android.support.v4.media.session.MediaSessionCompat;
@@ -57,6 +55,35 @@ public void createNotification() {
5755

5856
mediaSession = new MediaSessionCompat(mService, "GPMDP_MusicService");
5957

58+
sReactContext.runOnUiQueueThread(new Runnable() {
59+
@Override
60+
public void run() {
61+
mediaSession.setCallback(new MediaSessionCompat.Callback() {
62+
@Override
63+
public void onPlay() {
64+
mService.sendCommand("playback", "playPause");
65+
super.onPlay();
66+
}
67+
68+
@Override
69+
public void onPause() {
70+
mService.sendCommand("playback", "playPause");
71+
super.onPause();
72+
}
73+
74+
@Override
75+
public void onSkipToNext() {
76+
super.onSkipToNext();
77+
}
78+
79+
@Override
80+
public void onSkipToPrevious() {
81+
super.onSkipToPrevious();
82+
}
83+
});
84+
}
85+
});
86+
6087
mediaSession.setFlags(
6188
MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
6289
MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);

android/app/src/main/java/com/marshallofsound/gpmdp/remote/PlaybackAPIService.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,14 @@ public void emitEvent(String evName, String msg) {
4747
LocalBroadcastManager.getInstance(this).sendBroadcast(WebSocketMessage);
4848
}
4949

50-
private void sendCommand(String namespace, String method) {
51-
ws.sendText("{\"namespace\": \"" + namespace + "\", \"method\": \"" + method + "\"}");
50+
public void sendCommand(String namespace, String method) {
51+
sendCommand(namespace, method, "[]");
52+
}
53+
54+
public void sendCommand(String namespace, String method, String argArray) {
55+
if (ws != null) {
56+
ws.sendText("{\"namespace\": \"" + namespace + "\", \"method\": \"" + method + "\", \"arguments\": " + argArray + "}");
57+
}
5258
}
5359

5460
@Override

0 commit comments

Comments
 (0)