Android, setting alarm sound volume not working - android

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

Related

mediaPlayer.setVolume didn't work when a phone in a silence

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);

Changing stream volume of ALARM STREAM when call is being handled

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/

Set Android Volume In A Broadcast Receiver

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!

How to set notification sound volume programmatically?

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.

stop camera click sound programmatically in android

I am using android's default camera to click images from within my app. I want to stop the click sound that it does on clicking an image. Is there a way to stop that click sound programtically?
Thanks.
I was able to successfully use the trick to turn the volume down. I did this just before taking the picture:
AudioManager mgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
mgr.setStreamMute(AudioManager.STREAM_SYSTEM, true);
And this just after getting the first message back:
AudioManager mgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
mgr.setStreamMute(AudioManager.STREAM_SYSTEM, false);
To disable sound use this code:
AudioManager mgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
int streamType = AudioManager.STREAM_SYSTEM;
mgr.setStreamSolo(streamType, true);
mgr.setRingerMode(AudioManager.RINGER_MODE_SILENT);
mgr.setStreamMute(streamType, true);
To enable sound use this code:
mgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
streamType = AudioManager.STREAM_SYSTEM;
mgr.setStreamSolo(streamType, false);
mgr.setRingerMode(AudioManager.RINGER_MODE_SILENT);
mgr.setStreamMute(streamType, false);
For my Nexus S running 4.0.3, AudioManager.STREAM_SYSTEM does not prevent camera shutter sound from emitting,
but AudioManager.STREAM_MUSIC works!
You can mute and unmute your device using the code below:
final AudioManager mode = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
//Silent Mode Programatically
mode.setRingerMode(AudioManager.RINGER_MODE_SILENT);
//Normal Mode Programatically
mode.setRingerMode(AudioManager.RINGER_MODE_NORMAL);

Categories

Resources