How do you check if music is playing by using a broadcast receiver?
You don't need a broadcast receiver for this - AudioManager
is your friend:
AudioManager.isMusicActive()
does the job you want, have a closer look here for details: AudioManager
here is an example:
AudioManager manager = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
if(manager.isMusicActive())
{
// do something - or do it not
}
How to get info of currently playing music using broadcastreceiver while the app isn't running?
Try to set android:exported="true"
http://developer.android.com/guide/topics/manifest/receiver-element.html
android:exported
Whether or not the broadcast receiver can receive messages from sources outside its application — "true" if it can, and "false" if not. If "false", the only messages the broadcast receiver can receive are those sent by components of the same application or applications with the same user ID.
Receiving MP3 play actions in a BroadcastReceiver in Android
I would do neither. First, none of this is part of the SDK and so may change at any point. Second, this will only work for the built-in media player application, not any third-party or OEM-supplied media players, and I expect more people to gravitate to those.
how to detect when Music is played using Native Music Player?
I'll suggest a different approach, that I believe it's the correct approach.
the issue on your approach is that you're suggesting to check for one specific app. And there're tons of different music players, plus radio players, plus video players, plus games... and all of those should stop your music in case they want to play something.
So how you do it?
It's all explained in the Android Developers website.
You have to register an OnAudioFocusChangeListener
, so, whenever a different app request to have the audio focus, your app can stop the music.
Android broadcast intent for audio playback?
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);
}
}
}
}
Related Topics
Asynctask, Must It Take Such a Performance Penalty Hit...
Push Up Content When Clicking in Edit Text
Page Scroll When Soft Keyboard Popped Up
Android Shape Border with Gradient
How to Retrieve HTML Content from Webview (As a String)
How to Open Navigation Drawer with No Actionbar, Open with Just a Button
How to Apply Plugin to Only One Flavor in Gradle
Screen Overlay Detected Blocks Android Permissions
How to Load Class Androidcomponentsextension After Upgrading the Android Gradle Plugin 7.1
How to Use Getlistview() in Activity
Highlighting the Selected Item in the Listview in Android
Get Latitude and Longitude with Geocoder and Android Google Maps API V2
Lint Error "Do Not Treat Position as Fixed; Only Use Immediately..."
Android' Is Not Recognized as an Internal or External Command
Android Emulator Screen Fills Up Only Some Fraction Part of Actual Android Emulator Screen