stop camera click sound programmatically in android - 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);

Related

How can I get the Android phone speaker sound amplitude?

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

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/

Android, setting alarm sound volume not working

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

Android mute/unmute phone

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.

Categories

Resources