play loud ring tone/music irrespective of which profile it is in - android

i want to play any ring tone/song music irrespective of whether it is in silent mode or vibration mode or any other mode.
the code i am using to play a ring tone is
Uri ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
Ringtone ringtoneSound;
ringtoneSound=RingtoneManager.getRingtone(context, ringtoneUri);
if (ringtoneSound != null) {
ringtoneSound.play();
}
but ring tone plays only when mobile volume is high. is there any way to increase the volume of android device ? so that i can increase the volume of my android device and then run the above code.

This should work to set the volume if the phone is a profile authorizing sound.
public void setAlarmVolume(int iVol) {
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (am!=null) {
int maxVol = am.getStreamMaxVolume(AudioManager.STREAM_ALARM);
int volume = (maxVol*iVol)/100;
am.setStreamVolume(AudioManager.STREAM_ALARM, volume, 0);
}
}
iVol is the volume in percent (0 to 100).
Keep in mind that if the device is set on vibration only or on silence, you still won't get sound, and quite rightfully so. You can't bypass the user's wish not to have sound.

Related

Increase media player volume only when music is playing

I have audios which only play when certain conditions are met. I want to increase the audio volume only when it plays, and once it finishes the volume should be set to default system volume. Currently I am setting volume to max, which works ok when audio is playing but keeps the system volume to that level even after audios are finished. Which makes a very bad situation for user when say a call comes it makes the device very noisy.
I am confused to achieve this. I have also read that using audioManager to set volume is not a good practice and it has side effects.
Here is something which I was trying to control the volume:
public void resume() {
AudioManager audioManager = (AudioManager) MobieFitSdkApplication.singleton().getSystemService(Context.AUDIO_SERVICE);
if (player != null) {
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
player.setVolume(audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC), audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
}
}
So how can I only increase volume when audios are playing?
-Do one thing first of all get the current volume of System:
AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
int volume_level= am.getStreamVolume(AudioManager.STREAM_MUSIC);
after your audio complete or when you release media player at that time set the default volume which you get before the audio play.
am.setStreamVolume(AudioManager.STREAM_MUSIC,volume_level,0);
I have achieved what I have asked in my question by taking some help from the above answers.
First I am getting the original system volume and once the audio starts I am setting the system volume to 70%.
int originalVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
int maxDeviceVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float percent = 0.7f;
int seventyVolume = (int) (maxDeviceVolume*percent);
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, seventyVolume, 0);
Once the audio ends I am resetting the device volume to the original.
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, originalVolume, 0);

setStreamVolume() doesn't seem to work after turning Do not Disturb off

So I'm writing an app whose soul purpose is to turn the phone on full volume ringer mode, even from do not disturb mode. My app has notification filter change permission, so I am able to pull it out of DnD mode. Sweet. I've learned via logging that when I turn off DnD mode programmatically, ringer mode is set to normal, and ring volume is set to 1. But when I use setStreamVolume to max volume, it doesn't change the volume. Even more notably, I also can't manually turn the volume up via the volume rocker. The only way I have figured out how to increase the ring volume immediately after coming out of DnD mode is by using the volume slider on the screen. Another point is that when the ring volume is set to any value above 1 and then I test my app, setStreamVolume works. Am I missing something here? Here's some code.
Here is my receiver that detects when DnD mode is changed.
public void onReceive(Context context, Intent intent) {
if(intent.getAction() !=null && intent.getAction().equals(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED)) {
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (mNotificationManager.getCurrentInterruptionFilter() == NotificationManager.INTERRUPTION_FILTER_ALARMS) {
Log.i("NotificationFilter", "filter alarms");
} else if (mNotificationManager.getCurrentInterruptionFilter() == NotificationManager.INTERRUPTION_FILTER_ALL) {
AudioManager am = (AudioManager)context.getSystemService(AUDIO_SERVICE);
Log.i("NotificationFilter", "filter all, " + am.getStreamVolume(STREAM_RING));
int vol = am.getStreamMaxVolume(AudioManager.STREAM_RING);
am.setStreamVolume(AudioManager.STREAM_RING, vol, FLAG_SHOW_UI);
}
}
}

Manually mute sound in Android app when in silent mode?

I have tried finding an answer to this, but my searches kept returning irrelevant results...
So the problem is - I am writing a game that plays sound effects (no music just yet), and I set the stream type to Music. However, when I put my phone in silent mode (no vibration either, if that matters) the app still plays sounds. I can turn off the sound in the app using the volume keys, but what I expected (and what my future users would probably expect too) was that the app won't make a sound when the phone is in silent mode.
At this point, I am not sure if this should be automatically managed by the OS, or if I am expected to do something about it. I can surely detect the silent mode, set the volume to 0 in e.g onResume, but that will probably overwrite the setting the users set up by pressing the volume buttons - so when they unmute the phone, the app either has to set the volume programmatically to a predefined value, or ideally, to a saved one from sharedPrefs... which sounds relatively cumbersome. Is there a nicer solution?
This is how I initialize & then use sounds:
soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 100);
soundPoolMap = new SparseIntArray();
soundPoolMap.put(CLICK_1, soundPool.load(activity, R.raw.click1, 1));
soundPoolMap.put(CLICK_2, soundPool.load(activity, R.raw.click2, 2));
...
float volume = 1.0f;
soundPool.play(soundPoolMap.get(soundID), volume, volume, 1, 0, 1f);
What I understand from your question is you want to mute your app when device is on silent mode.
Try to check the the RingerMode every time before your app play sound/music and play sound only when the ringer RingerMode is normal.
Here is the sample code.
AudioManager audio = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
switch (audio.getRingerMode())
{
case AudioManager.RINGER_MODE_NORMAL:
// Device is on Normal mode.
// you can play music.
break;
case AudioManager.RINGER_MODE_SILENT:
// Device is on Silent mode.
// you should not play sound now.
break;
case AudioManager.RINGER_MODE_VIBRATE:
// Device is on Vibrate/Meeting mode.
// you should not play sound but you can make vibrate device (if you want).
break;
}

Play sounds in my application even though ringer is in silent mode

I have an application and I need to block the ringtone and notification sounds/vibrations, and play a custom sound while the phone is in incoming call state.
The way I'm trying to do that is to set the ringer mode into silent mode, when my application starts:
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT)
It does mute the ringtone and notifcation sounds.
However, when I'm trying to play my custom sound when the phone is incoming call state, using the music stream:
MediaPlayer mp= MediaPlayer.create(context, R.raw.my_ringtone);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setLooping(false);
mp.start();
It doesn't work on some devices (one of them is LG G2).
I'm sure there is a way to solve this issue, because other applications/games do manage to play sounds when the ringtone/notifications is in vibration mode.
Any help would be appreciated.
Thanks in advance!
So the another solution was you can play the ringtone here snippet given below
int volume = audioManager.getStreamVolume(AudioManager.STREAM_ALARM);
if(volume==0)
volume = audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM);
audioManager.setStreamVolume(AudioManager.STREAM_ALARM, volume,AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
ringtone = RingtoneManager.getRingtone(getApplicationContext(), Uri.parse(ringTonePath));
if(ringtone!=null){
ringtone.setStreamType(AudioManager.STREAM_ALARM);
ringtone.play();
isRinging = true;
}
isRinging set as flag if you want to programatically stop the playing or you can check the by isPlaying() of ringtone to stop playing.

Override silent mode and/or media volume

I want to override the silent mode and/or media volume to make the phone broadcast a loud noise. I know the alarm clock can override silent mode. How do you do this through your app?
Are you asking whether the Alarm Clock app can override silent mode or if code can override silent mode?
The code answer is yes, you can change the silent mode setting via code like this:
AudioManager audio = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
int currentVolume = audio.getStreamVolume(AudioManager.STREAM_RING);
int max = audio.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION);
audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
audio.setStreamVolume(AudioManager.STREAM_RING, max, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
I got another way, as the Tony's answer was worked but what happen it will change the profile suppose you put your device into the silent mode and app required to play any sound like alarm tone then it will change your profile silent to ringer mode and then again you need to put into silent mode. Am I right?
So the another solution was you can play the ringtone here snippet given below
int volume = audioManager.getStreamVolume(AudioManager.STREAM_ALARM);
if(volume==0)
volume = audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM);
audioManager.setStreamVolume(AudioManager.STREAM_ALARM, volume,AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
ringtone = RingtoneManager.getRingtone(getApplicationContext(), Uri.parse(ringTonePath));
if(ringtone!=null){
ringtone.setStreamType(AudioManager.STREAM_ALARM);
ringtone.play();
isRinging = true;
}
isRinging set as flag if you want to programatically stop the playing or you can check the by isPlaying() of ringtone to stop playing
going through this I didn't change the current profile and no more code for that
Prey is an open source phone tracker app which does this as one if its features - if you don't get a better answer could have a look at the source to see how they did it.

Categories

Resources