Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

audio recognition stream listen only once. #36

Open
taimoor522 opened this issue Jun 4, 2022 · 5 comments
Open

audio recognition stream listen only once. #36

taimoor522 opened this issue Jun 4, 2022 · 5 comments

Comments

@taimoor522
Copy link

taimoor522 commented Jun 4, 2022

Why does the stream only run once.....???

recognitionStream.listen((event) {
    print(listening');
    },
);
@Caldarie
Copy link
Owner

Caldarie commented Jun 4, 2022

To help me with your problem, please provide more information, such as logs and a sample of your code. If you cant provide your code, at least provide what parameters you have used in the stream.

@taimoor522
Copy link
Author

taimoor522 commented Jun 4, 2022

@Caldarie

recognitionStream = TfliteAudio.startAudioRecognition(
            sampleRate: 44100,
            bufferSize: 22016,
          );
audioSubscriber = recognitionStream.listen(
            (event) async {
              try {
                if (clapServiceStatus == true &&
                    event['recognitionResult'] == 'clap') {
                  // stop service when clap detected
                  UtilityFunctions.showPhoneFoundAlertDialog(
                      context, () => stopStartClapService(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);
                }
                if (prefs.getBool('whistleService') == true &&
                    event['recognitionResult'] == 'whistle') {
                  UtilityFunctions.showPhoneFoundAlertDialog(
                      context, () => stopStartWhistleService(context));

                  // if vibration is set to on then vibrate phone
                  bool whistleVib = prefs.getBool('whistleVibration') ?? false;
                  if (await (Vibration.hasVibrator()) == true && whistleVib) {
                    Vibration.vibrate(duration: 1000, amplitude: 255);
                  }

                  // if flashlight is set to on then turn flashlight
                  bool whistleFlash =
                      prefs.getBool('whistleFlashLight') ?? false;
                  if (whistleFlash) turnOnFlashLight();

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

recognitionStream listen-only run once. Why? If it is a stream then it should keep listening to the audio. I wrapped it inside a periodic timer to run it again and again.

@Caldarie
Copy link
Owner

Caldarie commented Jun 4, 2022

Can you provide the logs as well please.

@taimoor522
Copy link
Author

taimoor522 commented Jun 4, 2022

There is no need of logs.
The problem is simply that when I run the code below, it only prints the msg once even if it is a stream.

recognitionStream = TfliteAudio.startAudioRecognition(
            sampleRate: 44100,
            bufferSize: 22016,
          );
recognitionStream.listen((event) {
    print('msg');
    },
);

@Caldarie
Copy link
Owner

Caldarie commented Jun 4, 2022

With all due respect, logs give me a clearer picture as to whether the problem lies with the plugin or code. Help me help you, yes?

Also, you may need to read up more on how streams work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants