sir, is it possible to ring an alarm even when ringer is muted? if yes, how. and also, can i set its volume to something in between and not just maxvolume? thanks in advance
for (SmsMessage msg : messages) {
if (msg.getMessageBody().contains("firealert")) {
Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if(alert == null){
// alert is null, using backup
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if(alert == null){
// alert backup is null, using 2nd backup
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
}
}
Ringtone r = RingtoneManager.getRingtone(context.getApplicationContext(), alert);
AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
int maxVolumeAlarm = audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM);
int maxVolumeRing = audioManager.getStreamMaxVolume(AudioManager.STREAM_RING);
audioManager.setStreamVolume(AudioManager.STREAM_ALARM, maxVolumeAlarm,AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
audioManager.setStreamVolume(AudioManager.STREAM_RING, maxVolumeRing,AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
r.play();
Toast.makeText(context.getApplicationContext(), "alarm started", Toast.LENGTH_LONG).show();
}//end if
}//end for
Things to look at:
AudioManager.getRingerMode / setRingerMode. Set it to RINGER_MODE_NORMAL to ensure the phone isn't muted or set to vibrate. (Although you might want to check the state first, so you can store it and reset the value after your alarm is cleared.)
if ( aManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL )
aManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
If you want to set volume to 50% then check AudioManager.getStreamMaxVolume and then divide that by 2, for example. It would probably be better to let the user configure it, though. Use a SeekBar to have him set a preferred volume (which, again, you could reset back to normal after your alarm plays).
RingerVolumeBar = (SeekBar) findViewById(R.id.seekBar1);
RingerVolumeBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
#Override
public void onProgressChanged(SeekBar seekBar,
int progress, boolean fromUser)
{
aManager.setStreamVolume(AudioManager.STREAM_RING,
progress, AudioManager.FLAG_PLAY_SOUND);
// record new setting so you can load it up or reset it back later
...
}
});
Related
My alarm works well without silent and vibrate mode. When my phone is in silent and vibrate mode, How can I run the alarm. Also, how can I know alarm or music is finished.
private void mediaPlayerStart()
{
try
{
Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if(alert == null)
{
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
if(alert == null)
{
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
}
}
ringtone = RingtoneManager.getRingtone(getApplicationContext(), alert);
ringtone.play();
}
catch (Exception e)
{
e.printStackTrace();
}
}
Generally, if the user decided to mute his phone, it might be a good idea to keep it silent. However, if you're making an app for your own use, you can simply turn the volume up like so:
AudioManager mgr=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
mgr.setStreamVolume(AudioManager.STREAM_ALARM, mgr.getStreamMaxVolume( AudioManager.STREAM_ALARM), AudioManager.FLAG_PLAY_SOUND);
I am creating a notification with Android's NotificationManager.
Is it possible to 'override' the phone's volume (mute) settings in such a way, that the notification's sound is ALWAYS played?
The reason I need this is the following:
The notification is so important, that vibration alone may not be enough. The user MUST be alerted. So a sound shall be played, even if the phone is muted or the volume is very low.
yes it is possible,
MediaPlayer mMediaPlayer;
Uri notification = null;
notification = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_ALARM);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(ctx, notification);
// mMediaPlayer = MediaPlayer.create(ctx, notification);
final AudioManager audioManager = (AudioManager) ctx
.getSystemService(Context.AUDIO_SERVICE);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
mMediaPlayer.prepare();
// mMediaPlayer.start();
mMediaPlayer.setLooping(true);
mMediaPlayer.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer arg0) {
mMediaPlayer.seekTo(0);
mMediaPlayer.start();
}
});
You can change RINGING mode like this from silent to normal
AudioManager mobilemode = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
mobilemode.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
// Turn on all sound
// turn on sound, enable notifications
mobilemode.setStreamMute(AudioManager.STREAM_SYSTEM, false);
//notifications
mobilemode.setStreamMute(AudioManager.STREAM_NOTIFICATION, false);
//alarm
mobilemode.setStreamMute(AudioManager.STREAM_ALARM, false);
//ringer
mobilemode.setStreamMute(AudioManager.STREAM_RING, false);
//media
mobilemode.setStreamMute(AudioManager.STREAM_MUSIC, false);
// Turn off all sound
// turn off sound, disable notifications
mobilemode.setStreamMute(AudioManager.STREAM_SYSTEM, true);
//notifications
mobilemode.setStreamMute(AudioManager.STREAM_NOTIFICATION, true);
//alarm
mobilemode.setStreamMute(AudioManager.STREAM_ALARM, true);
//ringer
mobilemode.setStreamMute(AudioManager.STREAM_RING, true);
//media
mobilemode.setStreamMute(AudioManager.STREAM_MUSIC, true);
FOR YOUR CASE YOU CAN TRY SOMETHING LIKE THIS
int previousNotificationVolume =mobilemode.getStreamVolume(AudioManager.STREAM_NOTIFICATION);
mobilemode.setStreamVolume(AudioManager.STREAM_NOTIFICATION,mobilemode.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION), 0);
// Play notification sound
// Set notification sound to its previous
mobilemode.setStreamVolume(AudioManager.STREAM_NOTIFICATION,previousNotificationVolume, 0);
I have the following code in a BroadcastReciver and for some reason won't set the volume to 0.
If I put the same code that sets the volume attached to a button on click event it works just fine. Can someone shed some light as to why this might possibly happen? I know the broadcast receiver is being invoked because I put a log message inside the if statement and it displayed.
public void onReceive(Context context, Intent intent) {
int volume = CommonUtilities.getSharedPreferenceInt(context, CommonUtilities.PREF_FILE, CommonUtilities.PERCENT, -1);
boolean savedSettings = CommonUtilities.getSharedPreferenceBoolean(context, CommonUtilities.PREF_FILE, CommonUtilities.SAVED_SETTINGS, false);
if(volume >= 0 && savedSettings){
Log.v(CommonUtilities.TAG, "Setting the audio");
CommonUtilities.setSharedPreferenceBoolean(context, CommonUtilities.PREF_FILE, "SetAudio", true);
AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, AudioManager.FLAG_SHOW_UI);
}
}
If I put this same code in the onClick event it works just fine. The volume is set to 0 and shows the volume UI that it's set to 0.
AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, AudioManager.FLAG_SHOW_UI);
Thank you for helping!
I have a working alarm app, but wanted to add a feature where the user gets the choice between "Play alarm continuously till acknowledged" and "play alarm sound once".
I then looked at my alrm ringing code expecting to see some kind of "repeat" flag which I could optionally remove - but there was none. So how do I play the alarm sound just once?
My existing code looks like this:
private void playSound(Context context, Uri alert)
{
mMediaPlayer = new MediaPlayer();
try
{
mMediaPlayer.setDataSource(context, alert);
final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0)
{
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
mMediaPlayer.prepare();
mMediaPlayer.start();
}
}
catch (IOException e)
{
// oops!
}
}
Actually in each alarm sound there is a FLAG named ANDROID_LOOP which force your sound to loop. Unfortunatly you can't change that flag even using MediaPlayer.setLooping(false).
But you still can manually stop your player after a certain time. For example getDuration will give you the length of your sound.
int duration = mMediaPlayer.getDuration();
Runnable stopSoundRunnable = new Runnable() {
#Override
public void run() {
mMediaPlayer.stop();
}
};
mSoundHanlder.postDelayed(stopSoundRunnable, duration);
I have this method in my main activity
private void beep()
{
AudioManager manager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
manager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, 0,
AudioManager.FLAG_SHOW_UI + AudioManager.FLAG_PLAY_SOUND);
Uri notification = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(),
notification);
r.play();
}
As I understand, notification sound volume should be regulated by STREAM_NOTIFICATION. But notification always plays with the same volume despite that volume number in setStreamVolume method. Why is that?
I went another way. It's not exactly answer to my question but appropriate one. When I play notification in STREAM_MUSIC everything is fine. So notification plays exactly with volume I pass as parameter to the function
private void beep(int volume)
{
AudioManager manager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
manager.setStreamVolume(AudioManager.STREAM_MUSIC, volume, 0);
Uri notification = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
MediaPlayer player = MediaPlayer.create(getApplicationContext(), notification);
player.start();
}
First of all, I hope you realize that you are attempting to play two notifications right after each other, so there might be a conflict about that. AudioManager.FLAG_PLAY_SOUND and r.play() will both try playing the sound. It is usually enough to give user one type of information, either by UI or by playing the beep with new volume. I suggest you delete one of the flags. If you don't need any, just give it 0.
Coming to the main question, I am not sure if you can set volume level to 0. Try putting 1, like
manager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, 1, AudioManager.FLAG_SHOW_UI);
If you want to mute the notification, try using setStreamMute, which is equivalent to setting the volume to 0.
For my case eventually this seemed to be working.
It would make your audio volume relative to the stream current volume but it was useful for my need.
MediaPlayer player = MediaPlayer.create(getApplicationContext(), notificationUri);
player.setVolume(toneLevel, toneLevel);
toneLevel is between 0.0 - 1.0 so you don't even need to find the range of your stream..
Write a function as follow, or copy and paste the following,
public void ringtone() {
try {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
} catch (Exception e) {}
}
and call ringtone() when you want to make a beep notification.