Skip to content

[Camera] Audio is only recorded with first recording on specific iOS device (iPhone 7) #131553

Closed
flutter/packages
#7143

Description

@misos1

Is there an existing issue for this?

Steps to reproduce

I can still reproduce this issue on iphone 7 plus with ios 15.7.8 and flutter 3.10.6 and camera package version 0.10.5+2: #30689

Expected results

It should not happen.

Actual results

It happens.

Code sample

Same as in original issue. Also reproducible with code from: https://bettercoding.dev/flutter/tutorial-video-recording-and-replay/

Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

Logs

Logs
[Paste your logs here]

Flutter Doctor output

Doctor output

[✓] Flutter (Channel stable, 3.10.6, on macOS 13.5 22G74 darwin-x64)
[✓] Xcode - develop for iOS and macOS (Xcode 14.3.1)
[✓] Chrome - develop for the web
[✓] VS Code (version 1.80.1)
[✓] Connected device (3 available)
! Error: (null) needs to connect to determine its availability. Check the connection between the device and its companion iPhone, and the connection between the iPhone and Xcode. Both devices may also need
to be restarted and unlocked. (code 1)
[✓] Network resources

Activity

  1. dam-ease commented on Jul 31, 2023

    @dam-ease

    Hi, @misos1. Thanks for filing this issue. Using the code sample provided here https://github.com/bettercoding-dev/flutter-video, I can't seem to produce on the latest master and stable channels and also using the camera: ^0.10.5+2 and
    video_player: ^2.7.0. Can you retry on your end with the code sample to confirm this issue still reproduces?

    RPReplay_Final1690802278.MP4
  2. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Jul 31, 2023
  3. misos1 commented on Jul 31, 2023

    @misos1
    Author

    According to your video it seems that you used some newer iphone like 11. It is possible that this bug happens only on older devices like iphone 7.

    RPReplay_Final1690813936.mov
  4. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Jul 31, 2023
  5. huycozy commented on Aug 1, 2023

    @huycozy
    Member

    I can reproduce this issue on my device iPhone 7, iOS 15.7.2. Verified this with plugin example code.

    The issue only occurs if I keep recording with one camera. But if I switch to another camera (front or rear) between tests, the output video always contains sound.

    The issue is not reproducible on Android (Realme 6, Android 11).

    flutter doctor -v (stable and master)
    [✓] Flutter (Channel stable, 3.10.6, on macOS 13.5 22G74 darwin-x64, locale en-VN)
        • Flutter version 3.10.6 on channel stable at /Users/huynq/Documents/GitHub/flutter
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision f468f3366c (3 weeks ago), 2023-07-12 15:19:05 -0700
        • Engine revision cdbeda788a
        • Dart version 3.0.6
        • DevTools version 2.23.1
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 32.0.0)
        • Android SDK at /Users/huynq/Library/Android/sdk
        • Platform android-33, build-tools 32.0.0
        • ANDROID_HOME = /Users/huynq/Library/Android/sdk
        • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
        • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 15.0)
        • Xcode at /Applications/Xcode-beta.app/Contents/Developer
        • Build 15A5195m
        • CocoaPods version 1.11.3
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 2022.2)
        • Android Studio at /Applications/Android Studio.app/Contents
        • Flutter plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/9212-flutter
        • Dart plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/6351-dart
        • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)
    
    [✓] VS Code (version 1.80.2)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.68.0
    
    [✓] Connected device (2 available)
        • macOS (desktop) • macos  • darwin-x64     • macOS 13.5 22G74 darwin-x64
        • Chrome (web)    • chrome • web-javascript • Google Chrome 115.0.5790.114
    
    [✓] Network resources
        • All expected network resources are available.
    
    • No issues found!
    [!] Flutter (Channel master, 3.13.0-15.0.pre.13, on macOS 13.5 22G74 darwin-x64, locale en-VN)
        • Flutter version 3.13.0-15.0.pre.13 on channel master at /Users/huynq/Documents/GitHub/flutter_master
        ! Warning: `flutter` on your path resolves to /Users/huynq/Documents/GitHub/flutter/bin/flutter, which is not inside your current Flutter SDK checkout at
          /Users/huynq/Documents/GitHub/flutter_master. Consider adding /Users/huynq/Documents/GitHub/flutter_master/bin to the front of your path.
        ! Warning: `dart` on your path resolves to /Users/huynq/Documents/GitHub/flutter/bin/dart, which is not inside your current Flutter SDK checkout at
          /Users/huynq/Documents/GitHub/flutter_master. Consider adding /Users/huynq/Documents/GitHub/flutter_master/bin to the front of your path.
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision c851efaa3a (5 hours ago), 2023-07-31 22:40:26 -0400
        • Engine revision 791f505c8c
        • Dart version 3.2.0 (build 3.2.0-16.0.dev)
        • DevTools version 2.25.0
        • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and upgrades.
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 32.0.0)
        • Android SDK at /Users/huynq/Library/Android/sdk
        • Platform android-33, build-tools 32.0.0
        • ANDROID_HOME = /Users/huynq/Library/Android/sdk
        • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
        • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 15.0)
        • Xcode at /Applications/Xcode-beta.app/Contents/Developer
        • Build 15A5195m
        • CocoaPods version 1.11.3
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 2022.2)
        • Android Studio at /Applications/Android Studio.app/Contents
        • Flutter plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/9212-flutter
        • Dart plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/6351-dart
        • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)
    
    [✓] VS Code (version 1.80.2)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.68.0
    
    [✓] Connected device (3 available)
        • RMX2001 (mobile) • EUYTFEUSQSRGDA6D • android-arm64  • Android 11 (API 30)
        • macOS (desktop)  • macos            • darwin-x64     • macOS 13.5 22G74 darwin-x64
        • Chrome (web)     • chrome           • web-javascript • Google Chrome 115.0.5790.114
    
    [✓] Network resources
        • All expected network resources are available.
    
    ! Doctor found issues in 1 category.
  6. added
    platform-iosiOS applications specifically
    packageflutter/packages repository. See also p: labels.
    has reproducible stepsThe issue has been confirmed reproducible and is ready to work on
    team-iosOwned by iOS platform team
    and removed
    in triagePresently being triaged by the triage team
    on Aug 1, 2023
  7. 2 remaining items

  8. misos1 commented on Aug 22, 2023

    @misos1
    Author

    This is caused by interference between camera_avfoundation and video_player_avfoundation. Both use same application shared AVAudioSession. Implicitly in camera_avfoundation because AVCaptureSession.usesApplicationAudioSession defaults to true and also AVCaptureSession.automaticallyConfiguresApplicationAudioSession defaults to true and seems capture session in such case is setting AVAudioSession.category to AVAudioSessionCategoryPlayAndRecord and video_player_avfoundation sets this explicitly to AVAudioSessionCategoryPlayback:
    https://github.com/flutter/packages/blob/354af05222810a79ffc59201fddebc367a911d87/packages/video_player/video_player_avfoundation/ios/Classes/FLTVideoPlayerPlugin.m#L596

    Note this "per-instance" state which is actually global state, multiple video player controllers will share this across app:
    https://github.com/flutter/packages/blob/354af05222810a79ffc59201fddebc367a911d87/packages/video_player/video_player_avfoundation/ios/Classes/FLTVideoPlayerPlugin.m#L706-L710

    1. I think it would not be good idea to use separate AVAudioSession for recording as this could cause interruptions as is stated in:
      https://developer.apple.com/documentation/avfoundation/avcapturesession/1620490-usesapplicationaudiosession?language=objc

    2. Do not mess with global settings in plugins like video_player or camera. What would happen if I used yet another plugin which sets yet another audio category? Maybe better would be to have a separate plugin for that and the user will be required to set up a proper audio session category so that camera and video_player work properly like there are permission requirements already now. (This could be even done automatically where each plugin could hint its category and for example when both playback and record is required then will be selected PlayAndRecord.)

    3. Audio can be fixed and restored by removing and re-adding audioInput into _audioCaptureSession. This will also restore the category to PlayAndRecord. Problem is how to detect that there is no audio. For example it could be done after change to AVAudioSession.category but this is not even KVO compliant. The only working method of detection which I came with is really ugly. It looks at audio input sample buffer bytes and if all bytes are zero then it means we have no audio.

    I think the best would be 2 and maybe this is applicable also to android. Video player would work also without setting AVAudioSessionCategoryPlayback it will just not play audio when the phone is in silent mode (which can be actually more appropriate for some apps). But somehow video cannot be recorded on older iPhones when it is not set to AVAudioSessionCategoryPlayAndRecord (or possibly AVAudioSessionCategoryRecord) and on newer iPhones it probably works with any category. So probably would be the best to have a dedicated plugin/package that lets users set audio category, options and modes as is most appropriate for specific apps.

    Any other ideas?

  9. misos1 commented on Aug 22, 2023

    @misos1
    Author

    For now I would suggest to not set category AVAudioSessionCategoryPlayback in L596 if it is already PlayAndRecord or Playback. And use already set category in L706-L710. That should fix this issue.

  10. kaciula commented on Jan 21, 2024

    @kaciula

    Is there a workaround for this? I'm trying to film multiple videos one after the other and only the first one has sound.

  11. misos1 commented on Jul 8, 2024

    @misos1
    Author

    @kaciula You can try to do something like VideoPlayerController.file(File(""))..initialize()..dispose() before initializing camera so player will not later overwrite category set by camera.

  12. misos1 commented on Jul 13, 2024

    @misos1
    Author

    Also as camera_avfoundation does not set automaticallyConfiguresApplicationAudioSession to false, it automatically overwrites what was set by the mixWithOthers option of VideoPlayerOptions.

  13. github-actions commented on Jan 27, 2025

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  14. locked as resolved and limited conversation to collaborators on Jan 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work liste: device-specificOnly manifests on certain devicesfound in release: 3.10Found to occur in 3.10found in release: 3.13Found to occur in 3.13has reproducible stepsThe issue has been confirmed reproducible and is ready to work onp: cameraThe camera pluginpackageflutter/packages repository. See also p: labels.platform-iosiOS applications specificallyteam-iosOwned by iOS platform teamtriaged-iosTriaged by iOS platform team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions