Skip to content

Commit 86c0f86

Browse files
committed
feat: Disable shutter sound if in silent
1 parent 7d8d2b7 commit 86c0f86

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

package/android/src/main/java/com/mrousavy/camera/core/CameraSession.kt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ import android.annotation.SuppressLint
55
import android.content.Context
66
import android.content.pm.PackageManager
77
import android.graphics.BitmapFactory
8+
import android.media.AudioManager
89
import android.util.Log
910
import android.util.Range
1011
import android.util.Size
1112
import androidx.annotation.MainThread
1213
import androidx.annotation.OptIn
1314
import androidx.camera.core.Camera
1415
import androidx.camera.core.CameraControl
16+
import androidx.camera.core.CameraInfo
1517
import androidx.camera.core.CameraSelector
1618
import androidx.camera.core.CameraState
1719
import androidx.camera.core.DynamicRange
@@ -96,6 +98,7 @@ class CameraSession(private val context: Context, private val callback: Callback
9698
private val lifecycleRegistry = LifecycleRegistry(this)
9799
private var recording: Recording? = null
98100
private var isRecordingCanceled = false
101+
private val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
99102

100103
// Threading
101104
private val mainExecutor = ContextCompat.getMainExecutor(context)
@@ -481,8 +484,9 @@ class CameraSession(private val context: Context, private val callback: Callback
481484

482485
photoOutput.flashMode = flash.toFlashMode()
483486
photoOutput.targetRotation = outputOrientation.toSurfaceRotation()
487+
val enableShutterSoundActual = getEnableShutterSoundActual(enableShutterSound)
484488

485-
val photoFile = photoOutput.takePicture(context, enableShutterSound, metadataProvider, callback, CameraQueues.cameraExecutor)
489+
val photoFile = photoOutput.takePicture(context, enableShutterSoundActual, metadataProvider, callback, CameraQueues.cameraExecutor)
486490
val isMirrored = photoFile.metadata.isReversedHorizontal
487491

488492
val bitmapOptions = BitmapFactory.Options().also {
@@ -496,6 +500,20 @@ class CameraSession(private val context: Context, private val callback: Callback
496500
return Photo(photoFile.uri.path, width, height, orientation, isMirrored)
497501
}
498502

503+
private fun getEnableShutterSoundActual(enable: Boolean): Boolean {
504+
if (CameraInfo.mustPlayShutterSound()) {
505+
Log.i(TAG, "This phone must play a shutter sound due to regional restrictions, enabling shutter sound...")
506+
return true
507+
}
508+
509+
if (enable && audioManager.ringerMode != AudioManager.RINGER_MODE_NORMAL) {
510+
Log.i(TAG, "Ringer mode is silent (${audioManager.ringerMode}), disabling shutter sound...")
511+
return false
512+
}
513+
514+
return enable
515+
}
516+
499517
@OptIn(ExperimentalPersistentRecording::class)
500518
@SuppressLint("MissingPermission", "RestrictedApi")
501519
fun startRecording(

0 commit comments

Comments
 (0)