My app requires me to modify the volume of alarm stream when incoming call has been received, and voice transmission between two parties is going on.
I used this code:
audioManager.setStreamVolume(AudioManager.STREAM_ALARM,volume,AudioManager.FLAG_PLAY_SOUND);
Log.d("Track","Volume set to : "+audioManager.getStreamVolume(AudioManager.STREAM_ALARM));
The log message shows me that volume is being set properly. However, if I check alarm volume in the phone, it hasn't updated.
What should I do to make sure update happens properly in this scenario?
Try these snippets:
AudioManager mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
int maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolume, AudioManager.FLAG_PLAY_SOUND);
// or
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolume, AudioManager.FLAG_SHOW_UI);
// or
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolume, AudioManager.FLAG_VIBRATE);
Source: https://www.b4x.com/android/forum/threads/programatically-change-volume.7754/
Related
I'm trying to render a graph based on the music that is playing on Android phone. But I could not figure out a way to know how loud the song is currently playing. thanks!
// getSystemService is a Context method
AudioManager audioManager =
(AudioManager) getSystemService(Context.AUDIO_SERVICE);
// see other AudioManager.STREAM_* constants
int volume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
I customised an alarm clock, but it didn't work when the phone was in silent mode.
mediaPlayer = new MediaPlayer();
mediaPlayer = MediaPlayer.create(this, R.raw.example);
mediaPlayer.setVolume(1.0f, 1.0f);
mediaPlayer.start();
What could be the problem?
Volume of MediaPlayer depends on volume of AudioManager for STREAM_MUSIC.
For instance, set volume to max for stream music, before play your sound, and maybe after re-set value to old value (0):
This solution not respect choice of user. He doesn't to be disturb if he put device in silent mode !
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolume, 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 am trying to set alarm volume using this:
amanager.setStreamVolume(AudioManager.STREAM_ALARM, OneValue, 0);
It works in Android 4.1. The Alarm sliders moves acording to my values.
But it does not work in GibgerBread. Nothing happens. I mean, I don't get to modify the volume.
Any clue?
Try this
private AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
then override onKeyDown method and write as below :-
case KeyEvent.KEYCODE_VOLUME_UP:
am.adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_RAISE,AudioManager.FLAG_SHOW_UI);
break;
case KeyEvent.KEYCODE_VOLUME_DOWN:
am.adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_LOWER,AudioManager.FLAG_SHOW_UI);
break;
There are four kind of sound setting in android 1)Alarm 2)Music 3)Ring Tone 4)Notification First Create object of AudioManager amanager; IF you want to set Volume use this code
For Notification
AudioManager amanager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
amanager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, AudioManager.FLAG_SHOW_UI + AudioManager.FLAG_PLAY_SOUND);
For Alerm
amanager.setStreamVolume(AudioManager.STREAM_ALARM,AudioManager.FLAG_SHOW_UI + AudioManager.FLAG_PLAY_SOUND);
For Music
amanager.setStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.FLAG_SHOW_UI + AudioManager.FLAG_PLAY_SOUND);
For Ring Tone
amanager.setStreamVolume(AudioManager.STREAM_RING,AudioManager.FLAG_SHOW_UI + AudioManager.FLAG_PLAY_SOUND);
Look in the AudioManager class and try either of this
My goal is to support 2 operations:
mute phone (possibly with vibrations enabled/disabled), so when a call or sms is received it won't make noise
unmute phone and restore the volume to the state before muting phone
How can I do this? What permissions are required in AndroidManifest?
This is the permission for vibrate into the manifest file
<uses-permission android:name="android.permission.VIBRATE" />
this is for to put the device in silent mode with vibrate
AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
this is for to put into the ringing mode
AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_RING);
audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
audioManager.setStreamVolume(AudioManager.STREAM_RING, maxVolume, AudioManager.FLAG_SHOW_UI + AudioManager.FLAG_PLAY_SOUND);
public void changeRingerMode(Context context){
AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
/**
* To Enable silent mode.....
*/
audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);
/**
* To Enable Ringer mode.....
*/
audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}
If what you want is to disable sound and restore the sound setting to previous state, this worked for me.
static int ringstate = 0;
private void soundOn(boolean off){
AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
if(off)
{ //turn off ringing/sound
//get the current ringer mode
ringstate = audio.getRingerMode();
if(ringstate!=AudioManager.RINGER_MODE_SILENT)
audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);//turn off
}
else
{
//restore previous state
audio.setRingerMode(ringstate);
}
}
This should do.