3

I'm trying to build an Android app that uses a broadcast receiver to detect when music playback starts, and then offer gesture control to trigger sending a skip track intent back out.

To begin, I just want to set the receiver up to trigger a notification when audio playback starts so that I can check this concept will work. I've declared a service and a receiver in my manifest file, and I've created a service class that (hopefully) creates a notification via onCreate().

I've crawled a tonne of documentation though and can't seem to find the correct intent to listen in for with my receiver. Surely something exists for this?

Regards.

Ric
  • 458
  • 1
  • 7
  • 23

2 Answers2

4

It turns out there's a new security feature in Android 3.1 and above, where broadcast receivers don't register until the user manually opens the app at least once. Since I was trying to build a service-only app that had no UI to be opened, this was preventing all my intent listener attempts from working.

I've amended to have a splash-screen activity and now everything is working as I'd hoped. I'm now working to collate a list of player-specific intents to work from but for those trying to build something similar, com.android.music.playstatechanged seems to be the best start. This one is triggered by the default player (Google Play Music) when music either starts, pauses, or resumes and bundles itself with extra data including a boolean 'playing' property to differentiate between the play and pause states.

Essentially, this is the core receiver I'm now using. Have fun!

public class MainReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        // Start the service when music plays, stop the service when music ends.
        if(intent.hasExtra("playing")) {
            if(intent.getBooleanExtra("playing", false)) {
                Intent service = new Intent(context, MainService.class);
                context.startService(service);
            } else {
                Intent service = new Intent(context, MainService.class);
                context.stopService(service);
            }
        }
    }
}
Ric
  • 458
  • 1
  • 7
  • 23
  • 1
    Ric, could you please mention what to write in the manifest file to receive a broadcast that media is playing ? thanks. – Rat-a-tat-a-tat Ratatouille Mar 13 '14 at 10:01
  • Here you go, Rat-a-tat-a-tat Ratatouille. This should receive intents from the default Play Music app and from the official Last.fm app: http://pastebin.com/ycjDbq3H – Ric Mar 16 '14 at 00:27
0

Surely something exists for this?

There are thousands of music players. None are obligated to broadcast anything, let alone information about tracks. And none are obligated to allow you to "skip track" via any means.

You are welcome to search for specific music players that offer a documented and supported API for this sort of thing.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Hi CommonsWare, I should have been more specific - Most media players (Grooveshark, Last.fm, etc) seem to trigger the same lock-screen media widget - i.e. the one with the prev/next/play/pause buttons in Android 4, so I'm assuming these players use the MediaPlayer system, and it's these types of apps I want to support. – Ric Aug 11 '13 at 23:39
  • Oh, and I'm hoping to use com.android.music.musicservicecommand.next for the track skipping intent. As far as I can tell, this is what the lock screen widget sends out so fingers crossed I'll be able to send it out too :-) – Ric Aug 11 '13 at 23:46
  • @Ric: AFAIK, those aren't broadcasts, or if they are, they're not documented. Such media players register a `RemoteControlClient` with `AudioManager`. "Oh, and I'm hoping to use com.android.music.musicservicecommand.next for the track skipping intent" -- this is undocumented, unsupported, and does not have to be implemented by anyone. You're welcome to poke around some of the new [media routing stuff](https://developer.android.com/reference/android/support/v7/media/MediaRouter.html), though I don't know what versions of Android it supports. – CommonsWare Aug 11 '13 at 23:51
  • I do seem to be having a hard time finding documentation! Ref the musicservicecommand.next, I made a slight copy+paste error there and meant the almost-identical com.android.music.musicservicecommand.next intent. There doesn't seem to be much in the way of documentation, but https://github.com/android/platform_packages_apps_music/blob/master/src/com/android/music/MediaPlaybackService.java references it at line 101 and this older StackOverflow post claims it to wrok in Android 2.1, http://stackoverflow.com/questions/6921075/using-media-button-broadcasts-with-samsungs-default-media-player – Ric Aug 12 '13 at 00:02
  • @Ric: "There doesn't seem to be much in the way of documentation" -- that's because this broadcast is a private implementation detail of one app (the AOSP Music app). The mere fact that it exists does not mean that it has to be documented. That broadcast could be disabled at any point Google wants to, such as by using a `signature`-level permission to control who can send it. And, the AOSP Music app is one music app out of thousands. – CommonsWare Aug 12 '13 at 00:06