Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android broadcast intent for audio playback?

Tags:

android

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.

like image 924
Ric Avatar asked Oct 19 '25 12:10

Ric


1 Answers

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);
            }
        }
    }
}
like image 74
Ric Avatar answered Oct 22 '25 03:10

Ric



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!