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.
Related
I am trying to play Notification Sound without disturbing other Music playing
Notification Sound can be played by 2 ways
1) Played by OS itself via NotificationBuilder
Benifits:- Handle itself all the cases of not playing sound in Phone Calls and other cases as well
Problem:- When music is playing and notification comes, then it shifts audio focus from music to notification, there by lowering or silencing music depending on how that music player app has handled the loss in audio focus.
2) We play sound via our media Player whenever notification comes
A) Play on NOTIFICATION STREAM ---> works perfectly fine but gives probem of ducking when played via earphones
B) Play on SYSTEM STREAM
Benifits:- No shifting of audio focus, so no effect on music when notification comes
Problem:- We will have to handle other cases of not playing sound in Telephonic call and VOIP as well...and may be other not known cases as of now .
But I figured it that watsup is doing fine.
So don't know how it is doing it
Per the Android 5.0 behavior changes on notification sounds:
If you are currently adding sounds and vibrations to your notifications by using the Ringtone, MediaPlayer, or Vibrator classes, remove this code so that the system can present notifications correctly in priority mode. Instead, use Notification.Builder methods to add sounds and vibration.
Setting the device to RINGER_MODE_SILENT causes the device to enter the new priority mode. The device leaves priority mode if you set it to RINGER_MODE_NORMAL or RINGER_MODE_VIBRATE.
Previously, Android used STREAM_MUSIC as the master stream to control volume on tablet devices. In Android 5.0, the master volume stream for both phone and tablet devices is now unified, and is controlled by STREAM_RING or STREAM_NOTIFICATION.
As there is not a direct way to detect whether a device is in priority mode, you should always use the Notification.Builder/NotificationCompat.Builder methods to add sound to your notifications to ensure you meet the user's expectations.
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.
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?
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.
i have read for hours about how to start play a sound the way i want
but i cannot find an answer.
Here goes,
When device receive a C2DM push im displaying a Notification.
User can click the Notficationand my activity start.
Everything works grate.
Now I would like to create a phone-call-type of ring-sound
so it sounds like there's an incoming Voice call.
User pick up phone and notice my Notification presses it
and the sound stop playing.
Or even better, the C2DM message trigger my activity to start
with a phone-call-type of ring-sound.
When user touch the phone the sound stop playing
any help in any direction would be grate
Set the sound data member of your Notification to a Uri pointing to the ringtone you want to have played when the Notification is displayed. Ideally, you let the user pick their own ringtone (e.g., via a RingtonePreference) rather than forcing a certain ringtone on them.
Or even better, the C2DM message trigger my activity to start with a phone-call-type of ring-sound.
Popping up an activity like this, when the user might be in the middle of something else, is infrequently a good idea.