You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[camera_avfoundation] iOS: Fix crash when enableAudio == false by correcting guard condition (#9949)
## Summary
This PR fixes an iOS crash that occurs when using the `camera` plugin with `enableAudio: false` and no `NSMicrophoneUsageDescription` in `Info.plist`.
**Root cause:**
In `DefaultCamera.setUpCaptureSessionForAudioIfNeeded()`, the guard currently allows audio setup to proceed even when audio is disabled:
```swift
// current (buggy)
guard !mediaSettings.enableAudio || !isAudioSetup else { return }
```
This evaluates to `true` when `enableAudio == false`, so the audio setup runs regardless.
**Fix:** Require audio to be enabled **and** not already set up before proceeding:
```swift
// fixed
guard mediaSettings.enableAudio && !isAudioSetup else { return }
```
This aligns behavior with the intended logic (“don’t set up audio twice or when audio is disabled”) and prevents the crash path on iOS when the microphone usage description is absent.
- **Affected file:**
`camera_avfoundation/ios/camera_avfoundation/Sources/camera_avfoundation/DefaultCamera.swift`
- **Proposed correction commit:**
juliendelarbre@0635db4
- **Related Flutter issue & discussion:**
[flutter/flutter#174702 (comment)](flutter/flutter#174702 (comment))
---
## Before / After
**Before (buggy):** audio setup runs when `enableAudio == false`, causing a crash on iOS if `NSMicrophoneUsageDescription` is missing.
**After (fixed):** audio setup is skipped when `enableAudio == false`; no crash, behavior matches API expectations.
---
## Reproduction steps
1. Create a Flutter project targeting iOS.
2. Ensure `ios/Runner/Info.plist` does **not** contain `NSMicrophoneUsageDescription`.
3. Initialize `CameraController` with `enableAudio: false`.
4. Call `startVideoRecording()`.
5. Observe crash (on current main); with this PR applied, no crash.
---
## Linked Issues
Fixesflutter/flutter#174702
---
## Documentation
No API changes; behavior now matches the documented intent. (Optional: add a short inline comment elaborating that audio setup is skipped when `enableAudio == false`.)
---
## Pre-Review Checklist
Copy file name to clipboardExpand all lines: packages/camera/camera_avfoundation/ios/camera_avfoundation/Sources/camera_avfoundation/DefaultCamera.swift
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -340,7 +340,7 @@ final class DefaultCamera: NSObject, Camera {
340
340
341
341
func setUpCaptureSessionForAudioIfNeeded(){
342
342
// Don't setup audio twice or we will lose the audio.
0 commit comments