Canardoux / flutter_sound

Flutter plugin for sound. Audio recorder and player.
Mozilla Public License 2.0
875 stars 573 forks source link

[BUG] Recorder not working #517

Closed bowenchin closed 8 months ago

bowenchin commented 4 years ago

Flutter Sound Version :

Severity


Platforms you faced the error


Logs

flutter: startRecorder flutter: duration: 0 flutter: db: 0.00015999999999999999

Describe the bug Flutter Sound Recorder started and not null and behaves as expected on iOS simulator. However, when testing on physical iOS device, the recorder subscription outputs 0 for duration and db, which means that it is not properly recording audio on the device.

To Reproduce Steps to reproduce the behavior:

  1. Test and run app on physical iOS device
  2. Check db and duration from recorder on progress subscription, which outputs 0 instead of expected actual values from recording.
  3. Sound file not recorded and empty.

Expected behavior Sound Recorder should work as normal both on simulator and physical device.


Larpoux commented 4 years ago
bowenchin commented 4 years ago

Codec is pcm16WAV. There's no log since the recorderModule is running and not null, however running on physical iOS device returns 0 db and duration. This is more observational and as of now I do not know where to pinpoint error log as it doesn't return an exception or error.

BedRockGx commented 4 years ago

Hello, I have a similar error on the real IOS mobile phone. After the recording is turned on, monitor the method recorderModule.onProgress.listen () cannot listen to a valid recording time. Because of this, the recording cannot be ended and stored normally, resulting in the following errors:

Getting media information for /var/mobile/Containers/Data/Application/7CAAB0B4-A125-431D-BFC9-1054B5FA4AA6/Library/Caches/flutter sound-1604932777.aac. WARNING: Format aac detected only with low score of 1, misdetection possible! Format aac detected only with low score of 1, misdetection possible! /var/mobile/Containers/Data/Application/7CAAB0B4-A125-431D-BFC9-1054B5FA4AA6/Library/Caches/flutter sound-1604932777.aac: End of file ERROR: /var/mobile/Containers/Data/Application/7CAAB0B4-A125-431D-BFC9-1054B5FA4AA6/Library/Caches/flutter sound-1604932777.aac: End of file flutter: duration == 0.0 [VERBOSE-2:ui dart state.cc (177)] Unhandled Exception: NoSuchMethodError: The getter 'inMinutes' was called on null. Receiver: null

xSILENCEx commented 3 years ago

I have the same problem, duration aways 0 on IOS

Larpoux commented 3 years ago

We cannot do anything without more informations :

And everything else you think can be useful to understand your issue.

Did you correctly requested the Recording permission before calling startRecorder() ?

Larpoux commented 3 years ago

It seems that several users have problems with the recorder.

I really think that the problem is that the API verbs are not called correctly, but it is hard for me tou understand without any logs. Be aware that the extension of the file (for example foo.wav) has no impact on the encoder used. The encoder must be specified in the startRecorder() parameters.

I will try to return better errors when the parameters are incorrects. A crash is not a good error 😉

I keep this issue open, but please give more informations.

Larpoux commented 3 years ago

Just a simple question : are you sure to have granted the Recording Permission before doing startRecorder() ?

Actually there is something not good in Flutter Sound : Flutter Sound does not return a correct error when the Recording Permission is not granted. This will be fixed soon.

Larpoux commented 3 years ago

Please check that you correctly grant the Recording Permission, and re-open this issue if you have still problems.

mhstoller commented 3 years ago

@Larpoux

I believe I have been able to reproduce this issue consistently.

Steps to reproduce:

  1. Use openAudioSession with AudioFocus.requestFocus on an iOS device
  2. Record audio
  3. Playback audio (sometimes this is not required for the bug to occur, I'm not sure why)
  4. Try to record another audio file (it will have a duration of 0.00015999999999999999 and decibel level of 0)
openAudioSession(
        mode: SessionMode.modeSpokenAudio,
        focus: AudioFocus.requestFocus, //requestFocus may be related to the issue 
        category: SessionCategory.playAndRecord,
      );

When using AudioFocus.requestFocusAndDuckOthers, the problem does not occur.

The logs link on the documentation is broken (https://tau.canardoux.xyz/guides_logs.html), can you tell me how to get the logs?

|-- flutter_sound_lite 7.5.3+1
|   |-- flutter_sound_platform_interface 7.5.3+1
|   |-- flutter_sound_web 7.5.3+1
Larpoux commented 3 years ago

Thank you @mhstoller for your investigations. I am going to look to that next week. I am currently rewriting asynchronous processing done by τ. This is a major change and tricky. After the changes, we will be able to do :

myPlayer.openAudioSession(); // no need to wait
myPlayer.startPlayer(...); // no need to wait anymore the end of `openAudioSession()`

Also if the app is waiting on something (for example) :

await myPlayer.pausePlayer();

and the app does for example :

await myPlayer.pausePlayer(); // another call to the same function

or worst :

myPlayer.stopPlayer();

The first call to pausePlayer will receive an exception instead of being stuck for ever.

τ is in the middle of two asynchronous clients : the OS and the App. Managing asynchronous processing is really tough.

But I will look to your issue at the end of the week.

yanivshaked commented 3 years ago

Any update on the status of this issue, or any workaround for the meanwhile?

Thanks!

mhstoller commented 3 years ago

Any update on the status of this issue, or any workaround for the meanwhile?

When you open the audio session try using a different focus parameter such as AudioFocus.requestFocusAndDuckOthers

yanivshaked commented 3 years ago

As per your comment here, already tried but it didn't work.

yanivshaked commented 3 years ago

Specifying SessionCategory.record along with AudioFocus.requestFocusAndDuckOthers seems to be working:


    await _audioRecorder.openAudioSession(
      focus: AudioFocus.requestFocusAndDuckOthers,
      category: SessionCategory.record,
    );
Forander commented 2 years ago

I was having a similar issue and configuring the AudioSession seems to have done the trick.

Future initRecorder() async {
    final status = await Permission.microphone.request();

    if (status == PermissionStatus.granted) {
      recorder.setLogLevel(Level.error);

      await recorder.openRecorder();
      isRecorderReady = true;
    }
    if (status != PermissionStatus.granted) {
      throw 'Microphone permission not granted';
    }

    final session = await AudioSession.instance;
    await session.configure(AudioSessionConfiguration(
      avAudioSessionCategory: AVAudioSessionCategory.playAndRecord,
      avAudioSessionCategoryOptions:
          AVAudioSessionCategoryOptions.allowBluetooth |
              AVAudioSessionCategoryOptions.defaultToSpeaker,
      avAudioSessionMode: AVAudioSessionMode.spokenAudio,
      avAudioSessionRouteSharingPolicy:
          AVAudioSessionRouteSharingPolicy.defaultPolicy,
      avAudioSessionSetActiveOptions: AVAudioSessionSetActiveOptions.none,
      androidAudioAttributes: const AndroidAudioAttributes(
        contentType: AndroidAudioContentType.speech,
        flags: AndroidAudioFlags.none,
        usage: AndroidAudioUsage.voiceCommunication,
      ),
      androidAudioFocusGainType: AndroidAudioFocusGainType.gain,
      androidWillPauseWhenDucked: true,
    ));
  }
minhdangoz commented 2 years ago

I have the same issue, this is the log:

┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 2022-10-26 14:29:47.707500+0700 Runner[19412:7529240] flutter: │ #0 FlutterSoundRecorder.startRecorder (package:flutter_sound/public/flutter_sound_recorder.dart:590:13) 2022-10-26 14:29:47.707571+0700 Runner[19412:7529240] flutter: │ #1 _CameraLiveScreen.startIntervalRecording (camera_live_screen.dart:1072:10) 2022-10-26 14:29:47.707648+0700 Runner[19412:7529240] flutter: ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄ 2022-10-26 14:29:47.707716+0700 Runner[19412:7529240] flutter: │ 🐛 FS:---> startRecorder 2022-10-26 14:29:47.707794+0700 Runner[19412:7529240] flutter: └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 2022-10-26 14:29:47.708816+0700 Runner[19412:7529240] flutter: ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 2022-10-26 14:29:47.708905+0700 Runner[19412:7529240] flutter: │ #0 FlutterSoundRecorder._startRecorder (package:flutter_sound/public/flutter_sound_recorder.dart:614:13) 2022-10-26 14:29:47.708978+0700 Runner[19412:7529240] flutter: │ #1 FlutterSoundRecorder.startRecorder. (package:flutter_sound/public/flutter_sound_recorder.dart:592:13) 2022-10-26 14:29:47.709056+0700 Runner[19412:7529240] flutter: ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄ 2022-10-26 14:29:47.709125+0700 Runner[19412:7529240] flutter: │ 🐛 FS:---> _startRecorder. 2022-10-26 14:29:47.709197+0700 Runner[19412:7529240] flutter: └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 2022-10-26 14:29:47.722072+0700 Runner[19412:7529043] [aurioc] AURemoteIO.cpp:1139 failed: -10851 (enable 1, outf< 2 ch, 0 Hz, Float32, deinterleaved> inf< 2 ch, 0 Hz, Float32, deinterleaved>) 2022-10-26 14:29:47.722708+0700 Runner[19412:7529043] Terminating app due to uncaught exception 'Invalid Audio Session state', reason: 'The Audio Session is not in a correct state to do Recording.' First throw call stack: (0x19a0d2248 0x193497a68 0x19a1c1e04 0x1057327ac 0x10573340c 0x1057318f8 0x104a614f8 0x104a628cc 0x10913956c 0x108c11b2c 0x105d00598 0x105d0204c 0x105d12800 0x105d12344 0x19a162a08 0x19a144368 0x19a1491e4 0x1d2f69368 0x19c5f8d88 0x19c5f89ec 0x104a4c71c 0x1b846d948) libc++abi: terminating with uncaught exception of type NSException *** Terminating app due to uncaught exception 'Invalid Audio Session state', reason: 'The Audio Session is not in a correct state to do Recording.' terminating with uncaught exception of type NSException

minhdangoz commented 2 years ago

@Larpoux could you please help check above issue

JulyWitch commented 1 year ago

Same issue here. logs:

[aurioc]            AURemoteIO.cpp:1128  failed: -10851 (enable 1, outf< 2 ch,      0 Hz, Float32, deinterleaved> inf< 2 ch,      0 Hz, Float32, deinterleaved>)
*** Terminating app due to uncaught exception 'Invalid Audio Session state', reason: 'The Audio Session is not in a correct state to do Recording.'
*** First throw call stack:
(0x1806abd0c 0x197e9cee4 0x1806feef8 0x10adbaf6c 0x10adbbbcc 0x10adba0cc 0x102697098 0x102698458 0x126e619ac 0x126939e8c 0x18036b094 0x18036c094 0x18034d858 0x18034d4d0 0x1806670c4 0x1806245e8 0x180637240 0x1a1121988 0x182e3741c 0x182bd0b88 0x1026524c8 0x1028203d0)
libc++abi: terminating with uncaught exception of type NSException
* thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGABRT
    frame #0: 0x00000001baf0cbbc libsystem_kernel.dylib`__pthread_kill + 8
libsystem_kernel.dylib`:
->  0x1baf0cbbc <+8>:  b.lo   0x1baf0cbd8               ; <+36>
    0x1baf0cbc0 <+12>: stp    x29, x30, [sp, #-0x10]!
    0x1baf0cbc4 <+16>: mov    x29, sp
    0x1baf0cbc8 <+20>: bl     0x1baf0860c               ; cerror_nocancel
Target 0: (Runner) stopped.
Lost connection to device.
Exited
xiaoxin01 commented 1 year ago

I was having a similar issue and configuring the AudioSession seems to have done the trick.

Future initRecorder() async {
    final status = await Permission.microphone.request();

    if (status == PermissionStatus.granted) {
      recorder.setLogLevel(Level.error);

      await recorder.openRecorder();
      isRecorderReady = true;
    }
    if (status != PermissionStatus.granted) {
      throw 'Microphone permission not granted';
    }

    final session = await AudioSession.instance;
    await session.configure(AudioSessionConfiguration(
      avAudioSessionCategory: AVAudioSessionCategory.playAndRecord,
      avAudioSessionCategoryOptions:
          AVAudioSessionCategoryOptions.allowBluetooth |
              AVAudioSessionCategoryOptions.defaultToSpeaker,
      avAudioSessionMode: AVAudioSessionMode.spokenAudio,
      avAudioSessionRouteSharingPolicy:
          AVAudioSessionRouteSharingPolicy.defaultPolicy,
      avAudioSessionSetActiveOptions: AVAudioSessionSetActiveOptions.none,
      androidAudioAttributes: const AndroidAudioAttributes(
        contentType: AndroidAudioContentType.speech,
        flags: AndroidAudioFlags.none,
        usage: AndroidAudioUsage.voiceCommunication,
      ),
      androidAudioFocusGainType: AndroidAudioFocusGainType.gain,
      androidWillPauseWhenDucked: true,
    ));
  }

This solution is working for me.

Zeinab-Kouhkan commented 1 year ago

@Larpoux I have the same issue on iOS. flutter_sound: ^9.2.13 flutter sdk version: 3.7.10

This is the log:

2023-05-02 12:09:40.360876+0330 Runner[472:47934] flutter: ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 2023-05-02 12:09:40.361133+0330 Runner[472:47934] flutter: │ #0 FlutterSoundRecorder.startRecorder (package:flutter_sound/public/flutter_sound_recorder.dart:590:13) 2023-05-02 12:09:40.361231+0330 Runner[472:47934] flutter: │ #1 AudioRecorderUtils.record (package:zireh/utils/audio_recorder.dart:64:22) 2023-05-02 12:09:40.361323+0330 Runner[472:47934] flutter: ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄ 2023-05-02 12:09:40.361407+0330 Runner[472:47934] flutter: │ 🐛 FS:---> startRecorder 2023-05-02 12:09:40.361495+0330 Runner[472:47934] flutter: └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 2023-05-02 12:09:40.363440+0330 Runner[472:47934] flutter: ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 2023-05-02 12:09:40.363630+0330 Runner[472:47934] flutter: │ #0 FlutterSoundRecorder._startRecorder (package:flutter_sound/public/flutter_sound_recorder.dart:614:13) 2023-05-02 12:09:40.363734+0330 Runner[472:47934] flutter: │ #1 FlutterSoundRecorder.startRecorder. (package:flutter_sound/public/flutter_sound_recorder.dart:592:13) 2023-05-02 12:09:40.363861+0330 Runner[472:47934] flutter: ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄ 2023-05-02 12:09:40.364063+0330 Runner[472:47934] flutter: │ 🐛 FS:---> _startRecorder. 2023-05-02 12:09:40.364160+0330 Runner[472:47934] flutter: └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 2023-05-02 12:09:40.364960+0330 Runner[472:47934] flutter: ----------------FIREBASE CRASHLYTICS---------------- 2023-05-02 12:09:40.366437+0330 Runner[472:47934] flutter: Exception: Recorder is not open 2023-05-02 12:09:40.366814+0330 Runner[472:47934] flutter:

0 FlutterSoundRecorder._waitOpen (package:flutter_sound/public/flutter_sound_recorder.dart:321:7)

1 FlutterSoundRecorder._startRecorder (package:flutter_sound/public/flutter_sound_recorder.dart:615:11)

2 FlutterSoundRecorder.startRecorder. (package:flutter_sound/public/flutter_sound_recorder.dart:592:13)

3 BasicLock.synchronized (package:synchronized/src/basic_lock.dart:31:24)

4 FlutterSoundRecorder.startRecorder (package:flutter_sound/public/flutter_sound_recorder.dart:591:17)

5 AudioRecorderUtils.record (package:zireh/utils/audio_recorder.dart:64:22)

6 TicketDetailBloc.record (package:zireh/screens/support/ticket/detail/bloc.dart:311:20)

7 _TicketDetailInputAreaState.sendButton.. (package:zireh/screens/support/ticket/detail/input_area.dart:97:18)

8 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:253:24)

9 LongPressGestureRecognizer._checkLongPressStart (package:flutter/src/gestures/long_press.dart:723:11)

10 LongPressGestureRecognizer.didExceedDeadline (package:flutter/src/gestures/long_press.dart:618:5)

11 PrimaryPointerGestureRecognizer.didExceedDeadlineWithEvent (package:flutter/src/gestures/recognizer.dart:643:5)

12 PrimaryPointerGestureRecognizer.addAllowedPointer. (package:flutter/src/gestures/recognizer.dart:586:41)

13 _rootRun (dart:async/zone.dart:1390:47)

14 _CustomZone.run (dart:async/zone.dart:1300:19)

15 _CustomZone.runGuarded (dart:async/zone.dart:1208:7)

16 _CustomZone.bindCallbackGuarded. (dart:async/zone.dart:1248:23)

17 _rootRun (dart:async/zone.dart:1398:13)

18 _CustomZone.run (dart:async/zone.dart:1300:19)

19 _CustomZone.bindCallback. (dart:async/zone.dart:1232:23)

20 Timer._createTimer. (dart:async-patch/timer_patch.dart:18:15)

21 _Timer._runTimers (dart:isolate-patch/timer_impl.dart:398:19)

22 _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:429:5)

23 _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:192:26)

2023-05-02 12:09:40.368091+0330 Runner[472:47934] flutter: ---------------------------------------------------- 2023-05-02 12:09:52.170514+0330 Runner[472:47934] flutter: ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 2023-05-02 12:09:52.170771+0330 Runner[472:47934] flutter: │ #0 FlutterSoundRecorder.stopRecorder (package:flutter_sound/public/flutter_sound_recorder.dart:726:13) 2023-05-02 12:09:52.170858+0330 Runner[472:47934] flutter: │ #1 AudioRecorderUtils.stopRecorder (package:zireh/utils/audio_recorder.dart:72:32) 2023-05-02 12:09:52.170933+0330 Runner[472:47934] flutter: ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄ 2023-05-02 12:09:52.173290+0330 Runner[472:47934] flutter: │ 🐛 FS:---> stopRecorder 2023-05-02 12:09:52.173473+0330 Runner[472:47934] flutter: └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 2023-05-02 12:09:52.174929+0330 Runner[472:47934] flutter: ┌──────────────────────────────────────────────────────────────── 2023-05-02 12:09:52.174929+0330 Runner[472:47934] flutter: ┌───────────────────\342────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 2023-05-02 12:09:52.174929+0330 Runner[472:47934] flutter: ┌───────────────────\342─────────────────────────────────────────────────────────────────────────────────────\342\224─────────────── 2023-05-02 12:09:52.179329+0330 Runner[472:47934] flutter: │ #0 FlutterSoundRecorder._stopRecorder (package:flutter_sound/public/flutter_sound_recorder.dart:736:13) 2023-05-02 12:09:52.179442+0330 Runner[472:47934] flutter: │ #1 FlutterSoundRecorder.stopRecorder. (package:flutter_sound/public/flutter_sound_recorder.dart:729:17) 2023-05-02 12:09:52.179522+0330 Runner[472:47934] flutter: ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄\342\224┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄ 2023-05-02 12:09:52.179595+0330 Runner[472:47934] flutter: │ 🐛 FS:---> _stopRecorder 2023-05-02 12:09:52.179668+0330 Runner[472:47934] flutter: └───────────────┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄\342\224┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄ 2023-05-02 12:09:52.179595+0330 Runner[472:47934] flutter: │ 🐛 FS:---> _stopRecorder 2023-05-02 12:09:52.179668+0330 Runner[472:47934] flutter: └───────────────\342─────────────────────────────────────────────────────────────────────────────────────┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄\342\224┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄ 2023-05-02 12:09:52.179595+0330 Runner[472:47934] flutter: │ 🐛 FS:---> _stopRecorder 2023-05-02 12:09:52.179668+0330 Runner[472:47934] flutter: └───────────────\342─────────────────────────────────────────────────────────────────────────────────────\342\224─────────────────── 2023-05-02 12:09:52.180771+0330 Runner[472:47934] flutter: ┌─────────────────────────────────────────────┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄\342\224┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄ 2023-05-02 12:09:52.179595+0330 Runner[472:47934] flutter: │ 🐛 FS:---> _stopRecorder 2023-05-02 12:09:52.179668+0330 Runner[472:47934] flutter: └───────────────\342─────────────────────────────────────────────────────────────────────────────────────\342\224─────────────────── 2023-05-02 12:09:52.180771+0330 Runner[472:47934] flutter: ┌─────────────────────────────────────────────\342\224────────────────────────────────────────────────────────────────────────── 2023-05-02 12:09:52.181068+0330 Runner[472:47934] flutter: │ #0 FlutterSoundRecorder._stopRecorder (package:flutter_sound/public/flutter_sound_recorder.dart:742:15) 2023-05-02 12:09:52.181640+0330 Runner[472:47934] flutter: │ #1 FlutterSoundRecorder.stopRecorder. (package:flutter_sound/public/flutter_sound_recorder.dart:729:17) 2023-05-02 12:09:52.183991+0330 Runner[472:47934] flutter: ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄ 2023-05-02 12:09:52.184188+0330 Runner[472:47934] flutter: │ 🐛 <--- _stopRecorder : Recorder is not open 2023-05-02 12:09:52.184299+0330 Runner[472:47934] flutter: └───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

2023-05-02 12:09:52.186249+0330 Runner[472:47934] flutter: ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 2023-05-02 12:09:52.186419+0330 Runner[472:47934] flutter: │ #0 FlutterSoundRecorder.stopRecorder (package:flutter_sound/public/flutter_sound_recorder.dart:731:13) 2023-05-02 12:09:52.186494+0330 Runner[472:47934] flutter: │ #1 2023-05-02 12:09:52.186576+0330 Runner[472:47934] flutter: ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄ 2023-05-02 12:09:52.187992+0330 Runner[472:47934] flutter: │ 🐛 FS:<--- stopRecorder 2023-05-02 12:09:52.188096+0330 Runner[472:47934] flutter: └───────────────────────────────────────────────────────────────

and this is my code:

  FlutterSoundRecorder? mRecorder;
  static const theSource = AudioSource.microphone;

  AudioRecorderUtils(this.mRecorder) {
    _mPath = '${DateTimeUtils.fileNameFromCurrentTime}.m4a';
    openTheRecorder().then((value) {
      mRecorderIsInit = true;
    });
  }

  bool mRecorderIsInit = false;
  Codec _codec = Codec.aacMP4;
  late String _mPath;

  Future<void> openTheRecorder() async {
    if (!kIsWeb) {
      var status = await Permission.microphone.request();
      if (status != PermissionStatus.granted) {
        throw RecordingPermissionException('Microphone permission not granted');
      }
    }
    await mRecorder!.openRecorder();
    if (!await mRecorder!.isEncoderSupported(_codec) && kIsWeb) {
      _codec = Codec.opusWebM;
      _mPath = 'tau_file.webm';
      if (!await mRecorder!.isEncoderSupported(_codec) && kIsWeb) {
        mRecorderIsInit = true;
        return;
      }
    }

    final session = await AudioSession.instance;
    await session.configure(AudioSessionConfiguration(
      avAudioSessionCategory: AVAudioSessionCategory.playAndRecord,
      avAudioSessionCategoryOptions:
          AVAudioSessionCategoryOptions.allowBluetooth |
              AVAudioSessionCategoryOptions.defaultToSpeaker,
      avAudioSessionMode: AVAudioSessionMode.spokenAudio,
      avAudioSessionRouteSharingPolicy:
          AVAudioSessionRouteSharingPolicy.defaultPolicy,
      avAudioSessionSetActiveOptions: AVAudioSessionSetActiveOptions.none,
      androidAudioAttributes: const AndroidAudioAttributes(
        contentType: AndroidAudioContentType.speech,
        flags: AndroidAudioFlags.none,
        usage: AndroidAudioUsage.voiceCommunication,
      ),
      androidAudioFocusGainType: AndroidAudioFocusGainType.gain,
      androidWillPauseWhenDucked: true,
    ));

    mRecorderIsInit = true;
  }

  Future<void> record() async {
    await mRecorder!.startRecorder(
      toFile: _mPath,
      codec: _codec,
      audioSource: theSource,
    );
  }

  Future<String?> stopRecorder() async {
    var url = await mRecorder!.stopRecorder();
    return url;
  }
}
Larpoux commented 1 year ago

Perhaps you can try to configure your audio session for recoding, after opening your recorder.

Here is an extract of the flutter sound example:

Future<void> openTheRecorder() async {
    if (!kIsWeb) {
      var status = await Permission.microphone.request();
      if (status != PermissionStatus.granted) {
        throw RecordingPermissionException('Microphone permission not granted');
      }
    }
    await _mRecorder!.openRecorder();
    if (!await _mRecorder!.isEncoderSupported(_codec) && kIsWeb) {
      _codec = Codec.opusWebM;
      _mPath = 'tau_file.webm';
      if (!await _mRecorder!.isEncoderSupported(_codec) && kIsWeb) {
        _mRecorderIsInited = true;
        return;
      }
    }
    final session = await AudioSession.instance;
    await session.configure(AudioSessionConfiguration(
      avAudioSessionCategory: AVAudioSessionCategory.playAndRecord,
      avAudioSessionCategoryOptions:
          AVAudioSessionCategoryOptions.allowBluetooth |
              AVAudioSessionCategoryOptions.defaultToSpeaker,
      avAudioSessionMode: AVAudioSessionMode.spokenAudio,
      avAudioSessionRouteSharingPolicy:
          AVAudioSessionRouteSharingPolicy.defaultPolicy,
      avAudioSessionSetActiveOptions: AVAudioSessionSetActiveOptions.none,
      androidAudioAttributes: const AndroidAudioAttributes(
        contentType: AndroidAudioContentType.speech,
        flags: AndroidAudioFlags.none,
        usage: AndroidAudioUsage.voiceCommunication,
      ),
      androidAudioFocusGainType: AndroidAudioFocusGainType.gain,
      androidWillPauseWhenDucked: true,
    ));

    _mRecorderIsInited = true;
  }

In your pubspec.yaml you must add those dependencies:

  permission_handler: ^6.0.1
  audio_session: ^0.1.6
Larpoux commented 1 year ago

@Zeinab-Kouhkan I hope my previous remarks will help

kjwong commented 1 year ago

I'm also getting the same the audio session is not in a correct state to do recording issue on iOS and have tried the above code (opening recorder, configuring audio session, then recording). It doesn't crash every time, but it's crashing occasionally for users on prod and I haven't figured out how to reproduce it locally.

audio_session: ^0.1.6+1 flutter_sound: ^9.2.13 flutter: 3.7.6 permission_handler: ^10.0.0

mynlexi commented 1 year ago

We also wanted to upgrade to get the PR for this issue: https://github.com/Canardoux/flutter_sound/issues/885#issue-1223265158

but have the same issue of crashes in production Screenshot 2023-08-03 at 16 41 40

Not reproducible on the simulator.

github-actions[bot] commented 10 months ago

This issue is stale because it has been open 90 days with no activity. Leave a comment or this will be closed in 7 days.

garlicsoft commented 8 months ago
openRecorder

it work for me!

Larpoux commented 8 months ago

Ok. Thanks.

mahmoud-haj-ali commented 4 months ago
await _mRecorder!.openRecorder();

there is no openRecorder() function in the current version of the package,

is using audio_session package is enough?

Larpoux commented 4 months ago
  /// You may not openAudioSession many recorders without releasing them.
  ///
  /// `openRecorder()` and `closeRecorder()` return Futures.
  /// You do not need to wait the end of the initialization before [startRecorder()].
  /// [startRecorder] will automaticaly wait the end of `openRecorder()` before starting the recorder.
  ///
  /// *Example:*
  /// ```dart
  ///     myRecorder = await FlutterSoundRecorder().openRecorder();
  ///
  ///     ...
  ///     (do something with myRecorder)
  ///     ...
  ///
  ///     myRecorder.closeRecorder();
  ///     myRecorder = null;
  /// ```
Future<FlutterSoundRecorder?> openRecorder() async {
…
}