12

I am using this

camera.takePicture(null, rawCallback, jpegCallback);

but with some devices it makes a sound when the camera captures the image.

Please can any one help, how can I mute camera shutter sound?

Mark Davidson
  • 5,503
  • 5
  • 35
  • 54
PankajAndroid
  • 2,689
  • 3
  • 27
  • 41
  • I think this is somewhat undoable. At least in The States it was enforced by legislation to have all camera devices make a shutter sound. This is the case in some other countries too AFAIK. – harism Jan 23 '13 at 10:02
  • harism in some device it cause problem i had tested in nexus,s2,nexus table all device capture silently but htc desicer and some device cause problem. – PankajAndroid Jan 23 '13 at 10:15
  • is feeding the method with a silenced sound file a doable idea? – dumbfingers Jan 23 '13 at 10:29

5 Answers5

12

To mute, put this code before capturing an image

 AudioManager mgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
 mgr.setStreamMute(AudioManager.STREAM_SYSTEM, true);
 camera.takePicture(null, rawCallback, jpegCallback);

After 1 second it will unmute by putting in the below code:

 final Handler handler = new Handler();
 Timer t = new Timer();
 t.schedule(new TimerTask() {
    public void run() {
        handler.post(new Runnable() {
            public void run() {
            AudioManager mgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
            mgr.setStreamMute(AudioManager.STREAM_SYSTEM, false);
                    }
                });
            }
    }, 1000); 
StephenA
  • 61
  • 6
CoronaPintu
  • 1,865
  • 2
  • 17
  • 20
12

You can turn it off programmatically from 4.2 onwards with:

Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(id, info);
if (info.canDisableShutterSound) {
    mCamera.enableShutterSound(false);
}
Jono
  • 707
  • 2
  • 8
  • 17
7

Use Below two lines before button click

 AudioManager mgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
    mgr.setStreamMute(AudioManager.STREAM_SYSTEM, true);

And these two lones just after image get captured:

AudioManager mgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
mgr.setStreamMute(AudioManager.STREAM_SYSTEM, false);

I know only this solution and I personally used it in my application

Ajit
  • 957
  • 1
  • 8
  • 24
1

As harism mentioned, this is not possible to do on some devices, because there are legal requirements in some markets (Japan, for example) that taking a picture always results in an audible shutter sound.

The stream type used for these types of sounds is STREAM_SYSTEM_ENFORCED, and there's a read-only system property that determines whether or not you can mute such a stream.

Michael
  • 57,169
  • 9
  • 80
  • 125
  • Thanks micheal and harism it's solve by this way for mute: AudioManager mgr =(AudioManager)context.getSystemService(context.AUDIO_SERVICE); mgr.setStreamMute(AudioManager.STREAM_SYSTEM, true); for unmute: final Handler handler = new Handler(); Timer t = new Timer(); t.schedule(new TimerTask() { public void run() { handler.post(new Runnable() { public void run() { AudioManager mgr = (AudioManager)context.getSystemService(context.AUDIO_SERVICE); mgr.setStreamMute(AudioManager.STREAM_SYSTEM, false); } }); } }, 1000); – PankajAndroid Jan 23 '13 at 10:28
  • @PankajJolapara you can post your own solution as an answer and accept the answer that solves your problem. This could help anyone who has the same problem. :) – dumbfingers Jan 23 '13 at 10:31
  • There are devices for which the proposed solution won't work, since they're set to always route the shutter sound to the loudspeaker and not allow it to be muted. Again, it depends largely on the market where the device was sold. – Michael Jan 23 '13 at 10:41
0

Well, all those solutions are good but i prefer setting the volume to 0 (and not only system since some devices users other streams then system and/or not all streams include system) before:

smth like this would work flawlessly:

Camera mCamera = null;

function takePicture() {
   storeSoundSettings();
   setMuteAll(true);
   // init camera and such.
   Camera.CameraInfo info = new Camera.CameraInfo();
   Camera.getCameraInfo(IdOfCameraBackOrFront, info);
       if (info.canDisableShutterSound) {
           camera.enableShutterSound(false);
       }
   setMuteAll(false);
   recoverSoundSettings();
}

and store, recover and setMuteAll something like this:

int[] streams = new int[]{
                AudioManager.STREAM_ALARM,
                AudioManager.STREAM_DTMF,
                AudioManager.STREAM_MUSIC,
                AudioManager.STREAM_NOTIFICATION,
                AudioManager.STREAM_RING,
                AudioManager.STREAM_SYSTEM,
                AudioManager.STREAM_VOICE_CALL};
JSONObject json;
AudioManager manager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

public void storeSoundSettings() {
        json = new JSONObject();
        json.put("mode", manager.getMode());
        json.put("ringermode", manager.getRingerMode());

        for (int stream : streams) {
            json.put("stream_" + stream, manager.getStreamVolume(stream));
         }
}


public void recoverSoundSettings() {
        json = new JSONObject(readString("last_audio_setting", null));
        manager.setMode(json.getInt("mode"));
        manager.setRingerMode(json.getInt("ringermode"));

        for (int stream : streams) {
            manager.setStreamVolume(stream, json.getInt("stream_" + stream), AudioManager.FLAG_ALLOW_RINGER_MODES);
}

public void setMuteAll(boolean mute) {

    for (int stream : streams) {
        manager.setStreamMute(stream, mute);
        if (mute) {
            manager.setStreamVolume(stream, 100, AudioManager.FLAG_ALLOW_RINGER_MODES);
        } else {
            manager.setStreamVolume(stream, 0, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
        }
    }
}

Do not forget to catch the exceptions. I removed them for better highlighting.

akelec
  • 3,797
  • 3
  • 41
  • 39
Emanuel
  • 8,027
  • 2
  • 37
  • 56