How to detect music player states by using Broadcast Receiver - android

I am creating an application in which in have to detect music player states like playing, pause, stop.
i searched and found
AudioManager manager = (AudioManager) FlashLightActivity.this.getSystemService(Context.AUDIO_SERVICE);
if(manager.isMusicActive())
{
Toast.makeText(FlashLightActivity.this, "Music is playing..", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(FlashLightActivity.this, "Music has stopped..", Toast.LENGTH_LONG).show();
}
By using above code i am not able to detect music state if my application is in running mode.
Above code will check this condition at starting of the application. i wanna use broadcast receiver for detecting the state of background music.

Above code will check this condition at starting of the application
I will be stunned if it does so reliably. There is no requirement that any app tell AudioManager that it is playing music.
i wanna use broadcast receiver for detecting the state of background music.
There are thousands upon thousands of music players available for Android. There are no requirements that any of them implement broadcasts to tell you of the state of music playback. There are not even some standard broadcasts in the SDK that you could reasonably hope that some of those players might use.

Related

What is the best way to play an alarm ringtone when broadcast receiver is called?

I am having trouble finding the best way to activate an alarm tone. My broadcast receiver is called, the media player starts, but then stops. after about a second when my alarm app is closed and not open in recent apps. It will play continuously if the alarm app is open. Does anyone know the best way to play a sound once an alarm broadcast receiver goes off?
I found out what it was. It was apparently because my media player was getting destroyed by the garbage collector. So this answer https://stackoverflow.com/a/7719296/14727778 pointed out to use the media player as class level variable instead of declaring it in a method, so it does not get swept away. Pretty obvious! howd I miss that.....

Android: Is it possible to detect Alarm is ringing from android application?

I am developing a media player application in android. For that i need to know alarm is ringing or not. If alarm is ringing my application should pause music,As soon as alarm stopped ringing my application should play music again.
Please let me know any one have idea how to determine alarm is ringing or not from android application.
Here i added some investigations on this. But those are not suggest ways to use.
How to detect alarm ringing or other apps using speaker?
Listing of manufacturer's clock / alarm package and class name, Please add

How to Detect the Android Phone Speaker playing any sound

I want to detect if the Android Phone speaker is in use by any App like whether Music Player is playing Music or Alarm is Ringing or Video is playing. I dont want to detect just if a specific App is using the Ringer all i want to do is get Notified if the Ringer is used by Any APP.

Detect when music starts playing (or related events) without polling

My goal is to detect when music starts playing on the device. In my case I want to launch volume controls on an Android Wear device but that's irrelevant for the question.
I know there is AudioManager.isMusicActive() but it requires polling. I would rather listen for a broadcast without keeping a service alive indefinitely.
The other alternative would be to listen for headphones being plugged in but apparently Intent.ACTION_HEADSET_PLUG is only delivered to dynamic receivers as this answer suggests.
I'm a bit clueless here. How can I listen for any audio related events without constant polling?

Pause/resume mediaPlayer on notification sound

It's easy to detect when phone calls come in (via phoneStateListener), but what about other notification sounds, such as email or sms?
On some devices, these notification sounds mute but don't pause any currently running mediaPlayer instances, which is annoying for the user. Ideally, I'd like to listen for notifications that play sound, pause playback for the duration, and then resume playback afterwards.
You can get notified when another app wants to play audio by registering a callback on AudioManager.OnAudioFocusChangeListener (this would also handle the case of an incoming call). Specifically, you can look for AUDIOFOCUS_GAIN, AUDIOFOCUS_LOSS, and AUDIOFOCUS_TRANSIENT_LOSS. The Android music player source has a good example of this.

Categories

Resources