Canardoux / flutter_sound

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

onRecorderDbPeakChanged stops responding after startPlayerFromBuffer is called #246

Closed tonypottera24 closed 3 years ago

tonypottera24 commented 4 years ago

Version of flutter_sound

flutter_sound: ^2.1.1 and flutter_sound: ^3.0.0

flutter doctor

[✓] Flutter (Channel stable, v1.12.13+hotfix.8)
    • Flutter version 1.12.13+hotfix.8 
    • Framework revision 0b8abb4724 , 2020-02-11 11:44:36 -0800
    • Engine revision e1e6ced81d
    • Dart version 2.7.0

[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
    • Android SDK at ...
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-29, build-tools 29.0.2
    • ANDROID_HOME = ...
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b4-5784211)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 11.3.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 11.3.1, Build version 11C504
    • CocoaPods version 1.8.4

[✓] Android Studio (version 3.6)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 44.0.2
    • Dart plugin version 192.7761
    • Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b4-5784211)

[✓] VS Code (version 1.43.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.8.1

[✓] Connected device (1 available)
    • iPhone 11 • 9D52B61D-C2E7-46D0-9391-86778AE1B91C • ios • com.apple.CoreSimulator.SimRuntime.iOS-13-3 (simulator)

• No issues found!

Platforms you faced the error (IOS or Android or both?)

iOS

Expected behavior

onRecorderDbPeakChanged should keep returning values.

Actual behavior

onRecorderDbPeakChanged stops responding.

Tested environment (Emulator? Real Device?)

iOS 13.3 emulator

Steps to reproduce the behavior

FlutterSound _flutterSound = FlutterSound();
await _flutterSound.setDbLevelEnabled(true);
await _flutterSound.setDbPeakLevelUpdate(0.5);
await _flutterSound.startRecorder();
_flutterSound.onRecorderDbPeakChanged.listen((v){
print(v);
});
Uint8List _alarmSoundBuffer = (await rootBundle.load('assets/audio/desk_bell.m4a'))
        .buffer
        .asUint8List();
await _flutterSound.startPlayerFromBuffer(_alarmSoundBuffer);
kiha-la commented 4 years ago

I have not tested the code you gave, but I had a similar problem. When you try sound playback after recording, _channel.setMethodCallHandler called with some playback callback (like updateProgress).

However, _setRecorderCallback handles callbacks only updateRecorderProgress and updateDbPeakProgress, in addition, it throws the error to other callbacks.

  Future<void> _setRecorderCallback() async {
    if (_recorderController == null) {
      _recorderController = new StreamController.broadcast();
    }
    if (_dbPeakController == null) {
      _dbPeakController = new StreamController.broadcast();
    }

    _channel.setMethodCallHandler((MethodCall call) {
      switch (call.method) {
        case "updateRecorderProgress":
          Map<String, dynamic> result = json.decode(call.arguments);
          if (_recorderController != null)
            _recorderController.add(new RecordStatus.fromJSON(result));
          break;
        case "updateDbPeakProgress":
        if (_dbPeakController!= null)
          _dbPeakController.add(call.arguments);
          break;
        default:
          throw new ArgumentError('Unknown method ${call.method} ');
      }
      return null;
    });
  }

It looks like playback callback makes problem, but actually it caused by the recorder callback handler. (I was so complicated...:anguished:)

I got this problem in version 2.1.1, but I found _setRecorderCallback is modified on latest commit.

Why don't you give it a try if you are using older version?

tonypottera24 commented 4 years ago

@kiha-la Thank you for your advice, but the problem still exists in flutter_sound: ^3.0.0

tonypottera24 commented 4 years ago

This issue is fixed in 3.1.1. But currently, onRecorderDbPeakChanged is not responding on resumeRecorder.

Larpoux commented 4 years ago

I do not see any problem on iOS flutter_sound release 3.1.0

tonypottera24 commented 4 years ago

I'm using Samsung Galaxy S8, Android 9

tonypottera24 commented 4 years ago

move to #282

tonypottera24 commented 4 years ago

This problem happens again on flutter_sound: ^6.1.4. _flutterSoundRecorder.onProgress.listen(_recorderProgressChanged) stops responding after startPlayer is called from another audioSession.

tonypottera24 commented 4 years ago

I found out that given a FlutterSoundRecorder and a FlutterSoundPlayer. If their category are set to SessionCategory.record and SessionCategory.playback accordingly. On iPhone 6 Plus and iPhone 8 Plus, they will collide with each other. When FlutterSoundRecorder is recording, calling FlutterSoundPlayer can raise an exception and stop the recording. However, this situation doesn't happen on iPhone simulators and Android devices (Samsung Galaxy s8).

tonypottera24 commented 4 years ago

If we omit the SessionCategory setting (default is SessionCategory.playAndRecord), it works well on iPhone real devices. @Larpoux do you think this is a bug?

Larpoux commented 4 years ago

Yes Tony, you are right. The big problem is that on iOS there is just one audio-session for each App. It means that I have big problems to support Flutter Sound which allows several Audio Sessions simultaneously.

Actually it does not work very correctly, because the last open-session override the previous ones. I know that I will have to work to do something better than now, (or someone else 🙄 ). But not simple because of the limitations of iOS

tonypottera24 commented 4 years ago

Do you know why (sorry, just curious) when recording video, flutter_sound cannot record audio (I want to use voice meter) at the same time on Android, but this is possible on iOS?

Larpoux commented 4 years ago

Sorry, I do not know. I never tried, and I am definitely not an audio expert on ios or Android.

Larpoux commented 3 years ago

Please open new issues if still problems. This issue mixes several different points and so, cannot be identified as a TODO task.