Caldarie / flutter_tflite_audio

Audio classification Tflite package for flutter (iOS & Android). Can support Google Teachable Machine models
MIT License
63 stars 24 forks source link

startRecording() called on an uninitialized AudioRecord. #38

Open taimoor522 opened 2 years ago

taimoor522 commented 2 years ago
Future<Timer?> startListningClap(BuildContext context) async {
    // if service already running
    if (await FlutterForegroundTask.isRunningService) {
      setForceStopFlashlight(false);
      return Timer.periodic(const Duration(milliseconds: 500), (Timer ct) {
        try {
          clapAudioSubscriber.cancel();
        } catch (_) {}

        try {
          recognitionStream = TfliteAudio.startAudioRecognition(
            sampleRate: 44100,
            bufferSize: /*22016*/ 11016,
            detectionThreshold: 0.3,
          );
        } catch (_) {}

        // start listning to clap/whistle
        clapAudioSubscriber = recognitionStream.listen(
            (event) async {
              try {
                if (clapServiceStatus == true &&
                    event['recognitionResult'] == 'clap') {
                  // stop listening when clap detected

                  ct.cancel();
                  UtilityFunctions.showPhoneFoundAlertDialog(
                      context, () => stopStartClapListning(context));
                  // if vibration is set to on then vibrate phone
                  bool clapVib = prefs.getBool('clapVibration') ?? false;
                  if (await (Vibration.hasVibrator()) == true && clapVib) {
                    Vibration.vibrate(duration: 1000, amplitude: 255);
                  }

                  // if flashlight is set to on then turn flashlight
                  bool clapFlash = prefs.getBool('clapFlashLight') ?? false;
                  if (clapFlash) {
                    turnOnFlashLight();
                  }

                  // play melody if enabled by user
                  if (clapMelody == true) playMelody(volume);
                }
              } catch (_) {}
            },
            cancelOnError: true,
            onError: (_) {
              clapAudioSubscriber.cancel();
            },
            onDone: () {
              clapAudioSubscriber.cancel();
            });
      });
    }
    return null;
  }

E/AndroidRuntime(10013): Process: com.example.flutter_application_test, PID: 10013 E/AndroidRuntime(10013): java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord. E/AndroidRuntime(10013): at android.media.AudioRecord.startRecording(AudioRecord.java:1147) E/AndroidRuntime(10013): at flutter.tflite_audio.Recording.start(Recording.java:91) E/AndroidRuntime(10013): at flutter.tflite_audio.TfliteAudioPlugin.record(TfliteAudioPlugin.java:592) E/AndroidRuntime(10013): at flutter.tflite_audio.TfliteAudioPlugin.lambda$GvBCQqT11rP0XXTQzopagqcPxcA(Unknown Source:0) E/AndroidRuntime(10013): at flutter.tflite_audio.-$$Lambda$TfliteAudioPlugin$GvBCQqT11rP0XXTQzopagqcPxcA.run(Unknown Source:2) E/AndroidRuntime(10013): at java.lang.Thread.run(Thread.java:923) I/ExceptionHandle(10013): at android.media.AudioRecord.startRecording(AudioRecord.java:1147) I/ExceptionHandle(10013): at flutter.tflite_audio.Recording.start(Recording.java:91) I/ExceptionHandle(10013): at flutter.tflite_audio.TfliteAudioPlugin.record(TfliteAudioPlugin.java:592) I/ExceptionHandle(10013): at flutter.tflite_audio.TfliteAudioPlugin.lambda$GvBCQqT11rP0XXTQzopagqcPxcA(Unknown Source:0) I/ExceptionHandle(10013): at flutter.tflite_audio.-$$Lambda$TfliteAudioPlugin$GvBCQqT11rP0XXTQzopagqcPxcA.run(Unknown Source:2) I/ExceptionHandle(10013): at java.lang.Thread.run(Thread.java:923) D/TfliteAudio(10013): Parameters: {detectionThreshold=0.3, minimumTimeBetweenSamples=0, method=setAudioRecognitionStream, numOfInferences=1, averageWindowDuration=0, audioLength=0, sampleRate=44100, suppressionTime=0, bufferSize=11016} D/TfliteAudio(10013): AudioLength has been readjusted. Length: 44032 D/TfliteAudio(10013): Transpose Audio: false D/TfliteAudio(10013): Check for permission. Request code: 13 D/TfliteAudio(10013): Permission already granted.

Caldarie commented 2 years ago

It seems this is more an issue with the flutter foreground service than this plugin. Please bring the issue up with the author of that plugin.

otherwise, you can look at his example on how to implement the foreground service with tflite_audio here: https://github.com/Dev-hwang/tflite_audio_example