Skip to content

Commit

Permalink
feat: Add enableShutterSound prop to takePhoto() 🔊 (mrousavy#1702)
Browse files Browse the repository at this point in the history
* feat: Add `enableShutterSound` prop to `takePhoto()` 🔊

* Swift lint
  • Loading branch information
mrousavy authored Aug 21, 2023
1 parent 7595ff3 commit 2ca822c
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 4 deletions.
3 changes: 2 additions & 1 deletion android/src/main/java/com/mrousavy/camera/CameraSession.kt
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ class CameraSession(private val context: Context,

suspend fun takePhoto(qualityPrioritization: QualityPrioritization,
flashMode: Flash,
enableShutterSound: Boolean,
enableRedEyeReduction: Boolean,
enableAutoStabilization: Boolean,
outputOrientation: Orientation): CapturedPhoto {
Expand All @@ -197,7 +198,7 @@ class CameraSession(private val context: Context,
enableAutoStabilization,
orientation)
Log.i(TAG, "Photo capture 0/2 - starting capture...")
val result = captureSession.capture(captureRequest)
val result = captureSession.capture(captureRequest, enableShutterSound)
val timestamp = result[CaptureResult.SENSOR_TIMESTAMP]!!
Log.i(TAG, "Photo capture 1/2 complete - received metadata with timestamp $timestamp")
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ suspend fun CameraView.takePhoto(optionsMap: ReadableMap): WritableMap {
val flash = options["flash"] as? String ?: "off"
val enableAutoRedEyeReduction = options["enableAutoRedEyeReduction"] == true
val enableAutoStabilization = options["enableAutoStabilization"] == true
val enableShutterSound = options["enableShutterSound"] as? Boolean ?: true

val flashMode = Flash.fromUnionValue(flash)
val qualityPrioritizationMode = QualityPrioritization.fromUnionValue(qualityPrioritization)

val photo = cameraSession.takePhoto(qualityPrioritizationMode,
flashMode,
enableShutterSound,
enableAutoRedEyeReduction,
enableAutoStabilization,
outputOrientation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import android.hardware.camera2.CameraCaptureSession
import android.hardware.camera2.CaptureFailure
import android.hardware.camera2.CaptureRequest
import android.hardware.camera2.TotalCaptureResult
import android.media.MediaActionSound
import com.mrousavy.camera.CameraQueues
import com.mrousavy.camera.CaptureAbortedError
import com.mrousavy.camera.UnknownCaptureError
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine

suspend fun CameraCaptureSession.capture(captureRequest: CaptureRequest): TotalCaptureResult {
suspend fun CameraCaptureSession.capture(captureRequest: CaptureRequest, enableShutterSound: Boolean): TotalCaptureResult {
return suspendCoroutine { continuation ->
this.capture(captureRequest, object: CameraCaptureSession.CaptureCallback() {
override fun onCaptureCompleted(
Expand All @@ -20,9 +21,19 @@ suspend fun CameraCaptureSession.capture(captureRequest: CaptureRequest): TotalC
result: TotalCaptureResult
) {
super.onCaptureCompleted(session, request, result)

continuation.resume(result)
}

override fun onCaptureStarted(session: CameraCaptureSession, request: CaptureRequest, timestamp: Long, frameNumber: Long) {
super.onCaptureStarted(session, request, timestamp, frameNumber)

if (enableShutterSound) {
val mediaActionSound = MediaActionSound()
mediaActionSound.play(MediaActionSound.SHUTTER_CLICK)
}
}

override fun onCaptureFailed(
session: CameraCaptureSession,
request: CaptureRequest,
Expand Down
1 change: 1 addition & 0 deletions example/src/views/CaptureButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const _CaptureButton: React.FC<Props> = ({
qualityPrioritization: 'speed',
flash: flash,
quality: 90,
enableShutterSound: false,
}),
[flash],
);
Expand Down
5 changes: 4 additions & 1 deletion ios/CameraView+TakePhoto.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ extension CameraView {
photoSettings.flashMode = flashMode
}

// shutter sound
let enableShutterSound = options["enableShutterSound"] as? Bool ?? true

// depth data
photoSettings.isDepthDataDeliveryEnabled = photoOutput.isDepthDataDeliveryEnabled
if #available(iOS 12.0, *) {
Expand Down Expand Up @@ -75,7 +78,7 @@ extension CameraView {
photoSettings.isAutoContentAwareDistortionCorrectionEnabled = enableAutoDistortionCorrection
}

photoOutput.capturePhoto(with: photoSettings, delegate: PhotoCaptureDelegate(promise: promise))
photoOutput.capturePhoto(with: photoSettings, delegate: PhotoCaptureDelegate(promise: promise, enableShutterSound: enableShutterSound))

// Assume that `takePhoto` is always called with the same parameters, so prepare the next call too.
photoOutput.setPreparedPhotoSettingsArray([photoSettings], completionHandler: nil)
Expand Down
11 changes: 10 additions & 1 deletion ios/PhotoCaptureDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,22 @@ private var delegatesReferences: [NSObject] = []

class PhotoCaptureDelegate: NSObject, AVCapturePhotoCaptureDelegate {
private let promise: Promise
private let enableShutterSound: Bool

required init(promise: Promise) {
required init(promise: Promise, enableShutterSound: Bool) {
self.promise = promise
self.enableShutterSound = enableShutterSound
super.init()
delegatesReferences.append(self)
}

func photoOutput(_: AVCapturePhotoOutput, willCapturePhotoFor _: AVCaptureResolvedPhotoSettings) {
if !enableShutterSound {
// disable system shutter sound (see https://stackoverflow.com/a/55235949/5281431)
AudioServicesDisposeSystemSoundID(1108)
}
}

func photoOutput(_: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
defer {
delegatesReferences.removeAll(where: { $0 == self })
Expand Down
6 changes: 6 additions & 0 deletions src/PhotoFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ export interface TakePhotoOptions {
* @default false
*/
enableAutoDistortionCorrection?: boolean;
/**
* Whether to play the default shutter "click" sound when taking a picture or not.
*
* @default true
*/
enableShutterSound?: boolean;
}

/**
Expand Down

0 comments on commit 2ca822c

Please sign in to comment.