I have an android car stereo which has a seperate bluetooth app which controls the audio playback from a connected device. I am building a seperate app which runs on the car stereo which should have buttons to play/pause the audio from my phone.The problem is that I cannot play/pause the music from my app.
I have tried this approach but I am only able to control other music apps such as YouTube Music with this code:
package com.dashcam;
import android.content.Context;
import android.media.AudioManager;
import android.media.session.PlaybackState;
import android.os.SystemClock;
import android.util.Log;
import android.view.KeyEvent;
import androidx.annotation.NonNull;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
public class MediaModule extends ReactContextBaseJavaModule {
MediaModule(ReactApplicationContext context) {
super(context);
}
@NonNull
@Override
public String getName() {
return "MediaModule";
}
@ReactMethod
public void playPauseMedia() {
AudioManager audioManager = (AudioManager) getReactApplicationContext().getSystemService(Context.AUDIO_SERVICE);
if (audioManager != null) {
long eventTime = SystemClock.uptimeMillis();
KeyEvent downEvent = new KeyEvent(eventTime, eventTime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, 0);
KeyEvent upEvent = new KeyEvent(eventTime, eventTime, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, 0);
audioManager.dispatchMediaKeyEvent(downEvent);
audioManager.dispatchMediaKeyEvent(upEvent);
}
}
@ReactMethod
public void skipToNextTrack() {
AudioManager audioManager = (AudioManager) getReactApplicationContext().getSystemService(Context.AUDIO_SERVICE);
if (audioManager != null) {
audioManager.dispatchMediaKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT));
audioManager.dispatchMediaKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_NEXT));
}
}
@ReactMethod
public void skipToPreviousTrack() {
AudioManager audioManager = (AudioManager) getReactApplicationContext().getSystemService(Context.AUDIO_SERVICE);
if (audioManager != null) {
audioManager.dispatchMediaKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PREVIOUS));
audioManager.dispatchMediaKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PREVIOUS));
}
}
@ReactMethod
public void getPlaybackState(Promise promise) {
AudioManager audioManager = (AudioManager) getReactApplicationContext().getSystemService(Context.AUDIO_SERVICE);
if (audioManager != null) {
int playbackState = audioManager.isMusicActive() ? PlaybackState.STATE_PLAYING : PlaybackState.STATE_PAUSED;
promise.resolve(playbackState);
} else {
promise.reject("AUDIO_SERVICE_UNAVAILABLE", "Audio service unavailable");
}
}
}
Is there a way to send commands to my phone to play/pause the music from my seperate app?
I tried using AudioManager to send the commands to control the music playback but that only works on music players such as YouTube Music not with the bluetooth app.