@@ -5,13 +5,15 @@ import android.annotation.SuppressLint
5
5
import android.content.Context
6
6
import android.content.pm.PackageManager
7
7
import android.graphics.BitmapFactory
8
+ import android.media.AudioManager
8
9
import android.util.Log
9
10
import android.util.Range
10
11
import android.util.Size
11
12
import androidx.annotation.MainThread
12
13
import androidx.annotation.OptIn
13
14
import androidx.camera.core.Camera
14
15
import androidx.camera.core.CameraControl
16
+ import androidx.camera.core.CameraInfo
15
17
import androidx.camera.core.CameraSelector
16
18
import androidx.camera.core.CameraState
17
19
import androidx.camera.core.DynamicRange
@@ -96,6 +98,7 @@ class CameraSession(private val context: Context, private val callback: Callback
96
98
private val lifecycleRegistry = LifecycleRegistry (this )
97
99
private var recording: Recording ? = null
98
100
private var isRecordingCanceled = false
101
+ private val audioManager = context.getSystemService(Context .AUDIO_SERVICE ) as AudioManager
99
102
100
103
// Threading
101
104
private val mainExecutor = ContextCompat .getMainExecutor(context)
@@ -481,8 +484,9 @@ class CameraSession(private val context: Context, private val callback: Callback
481
484
482
485
photoOutput.flashMode = flash.toFlashMode()
483
486
photoOutput.targetRotation = outputOrientation.toSurfaceRotation()
487
+ val enableShutterSoundActual = getEnableShutterSoundActual(enableShutterSound)
484
488
485
- val photoFile = photoOutput.takePicture(context, enableShutterSound , metadataProvider, callback, CameraQueues .cameraExecutor)
489
+ val photoFile = photoOutput.takePicture(context, enableShutterSoundActual , metadataProvider, callback, CameraQueues .cameraExecutor)
486
490
val isMirrored = photoFile.metadata.isReversedHorizontal
487
491
488
492
val bitmapOptions = BitmapFactory .Options ().also {
@@ -496,6 +500,20 @@ class CameraSession(private val context: Context, private val callback: Callback
496
500
return Photo (photoFile.uri.path, width, height, orientation, isMirrored)
497
501
}
498
502
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
+
499
517
@OptIn(ExperimentalPersistentRecording ::class )
500
518
@SuppressLint(" MissingPermission" , " RestrictedApi" )
501
519
fun startRecording (
0 commit comments