Can I skip a track from an Android app?

I plan to make an application for Android 2.1 that changes the song every minute (through what I hope exists in Android, “next”) for the application using the atm audio device.

So, if my Spotify is already running in the background, playing music, can I transfer the program to the next track?

Let me know if I do not understand anything. Thanks in advance!

+3
source share
5 answers

There is no universal audio transport API for music apps, so you'll need to see if the music apps you are targeting will publicly reveal the bindings or intentions of the service. If not, you cannot do this.

+1
source

I know this is a little old question, but it took me a while to find something else that was mentioned here.

There is a workaround - the action of the multimedia message broadcast button. There is one receiver-receiver that can recognize if there was a broadcast from a system or from another application, so they can ignore non-system broadcasts.

Intent i = new Intent(Intent.ACTION_MEDIA_BUTTON);
synchronized (this) {
            i.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT));
            sendOrderedBroadcast(i, null);

            i.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_NEXT));
            sendOrderedBroadcast(i, null);
 }
+18
source

dispatchMediaKeyEvent() AudioManager KeyEvent, SDK.

+1

" " :

    final ComponentName serviceName = new ComponentName(context, 
        MediaPlaybackService.class);
    intent = new Intent(MediaPlaybackService.NEXT_ACTION);
    intent.setComponent(serviceName);
    pendingIntent = PendingIntent.getService(context,
            0 /* no requestCode */, intent, 0 /* no flags */);
    views.setOnClickPendingIntent(R.id.control_next, pendingIntent);

, , MediaPlaybackService . , , , .

, , . Spotify/Pandora/Last.fm , - .

0

It seems like you can use AudioManager to enter media keys.

Here is a snippet from another question

this.mAudioManager = (AudioManager) this.context.getSystemService(Context.AUDIO_SERVICE);

long eventtime = SystemClock.uptimeMillis();

KeyEvent downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT, 0);
mAudioManager.dispatchMediaKeyEvent(downEvent);

KeyEvent upEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_NEXT, 0);
mAudioManager.dispatchMediaKeyEvent(upEvent);

In the same way, you can enter the PlayPause button and some others. I tested it in the background service controlling Youtube and it worked on Android 6

0
source

Source: https://habr.com/ru/post/1741591/


All Articles