android mute camera shutter sound?

后端 未结 5 413
梦如初夏
梦如初夏 2020-12-09 19:11

I am using this

camera.takePicture(null, rawCallback, jpegCallback);

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

相关标签:
5条回答
  • 2020-12-09 19:47

    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.

    0 讨论(0)
  • 2020-12-09 20:07

    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

    0 讨论(0)
  • 2020-12-09 20:08

    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);
    }
    
    0 讨论(0)
  • 2020-12-09 20:08

    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.

    0 讨论(0)
  • 2020-12-09 20:14

    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); 
    
    0 讨论(0)
提交回复
热议问题