D
D
Dmitry2014-01-02 17:58:34
Android
Dmitry, 2014-01-02 17:58:34

How to manage third-party players in Android?

Let's say there is a bluetoh headset. It has buttons "next track", "previous track, pause / play next". She communicates with the phone, giving some commands to the current (!) Playing player. But how to repeat this?
Interested in compatibility with 2.3.3 and higher.
Tried two option The first one works only with the default player and not on all phones.

Intent i = new Intent("com.android.music.musicservicecommand");
i.putExtra("command" , key ); //key - "togglepause","next","previous"
context.sendBroadcast(i);

The second one works on all phones, but if several players are installed - it controls not the current one, but only one of them, by what principle the player is selected - I have no idea. That is, if another player is playing, it is paused, and the command is given to a completely different one, which is launched.
long eventtime = SystemClock.uptimeMillis() - 1;

//key - это KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, KeyEvent.KEYCODE_MEDIA_NEXT, KeyEvent.KEYCODE_MEDIA_PREVIOUS

Intent downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
KeyEvent downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, key, 0);
downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent);
context.sendOrderedBroadcast(downIntent, null);

eventtime++;
Intent upIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
KeyEvent upEvent = new KeyEvent(eventtime, eventtime,KeyEvent.ACTION_UP, key, 0);
upIntent.putExtra(Intent.EXTRA_KEY_EVENT, upEvent);
context.sendOrderedBroadcast(upIntent, null);

Here, I'm looking for a universal opportunity that works stably.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mik_os, 2014-01-03
@mik_os

developer.android.com/reference/android/media/Remo...
Usage example: https://github.com/CyanogenMod/android_frameworks_...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question