简体   繁体   English

android静音相机快门声?

[英]android mute camera shutter sound?

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? 请任何人帮忙,我怎样才能将相机快门声静音?

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: 1秒后,它将通过输入以下代码取消静音:

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

You can turn it off programmatically from 4.2 onwards with: 您可以通过以下方式从4.2开始以编程方式将其关闭:

Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(id, info);
if (info.canDisableShutterSound) {
    mCamera.enableShutterSound(false);
}

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 我只知道这个解决方案,我个人在我的应用程序中使用它

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. 用于这些类型声音的流类型是STREAM_SYSTEM_ENFORCED ,并且有一个只读系统属性,用于确定是否可以将此类流静音。

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: 好吧,所有这些解决方案都很好,但我更喜欢将卷设置为0(并且不仅系统,因为某些设备用户其他流然后是系统和/或不是所有流包括系统)之前:

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: 并存储,恢复和setMuteAll这样的事情:

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. 我删除了它们以便更好地突出显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM