是否可以使用“speech_to_text”库在用户句子结束后设置结束音频输入的时间?

问题描述 投票:0回答:1

我正在编写一段代码来识别用户的语音,并据此在应用程序中做出决策,但我们知道,当我们说话时,我们可以短暂中断以更好地思考单词,这就是我的问题开始的时候,当我剪掉我的句子,等一下,我开始说话,程序取消音频输入,并且不听第二部分,我想知道“speech_to_text”库中是否有任何我可以为其定义的方法在用户讲话结束后等待至少 3 秒,以确保捕获您的短语不会出现错误。

这是我处理语音的后端代码:

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:speech_to_text/speech_to_text.dart';

class Speech {
    static final _speech = SpeechToText();
    static Timer? _timer;
    static Future<bool> toggleRecording({required Function(String text) onResult, required ValueChanged<bool> onListening}) async {
      final isAvailable = await _speech.initialize(
        onStatus: (status) {
          return onListening(_speech.isListening);
        },
        onError: (error) {
          print('Error $error');
        },

      );
      if (isAvailable) {
        _speech.listen(
          partialResults: true,
          onResult: (value) {
            onResult(value.recognizedWords);
            print(onResult(value.recognizedWords));

          },
          listenFor: Duration(seconds: 10),
        );
      }
      return isAvailable;
    }
  }

这是我的 UI 前端代码:

import 'package:flutter/material.dart';
import 'package:speech_to_text/speech_to_text.dart';

class HomePage extends StatefulWidget {
    const HomePage({super.key});

    @override
    State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
    String textSample = 'Click button to start recording', _recognizedText = "";
    bool isListening = false;
    final  _speech = SpeechToText();
 
    void startListening() {
      _speech.listen(onResult: (val) {
        setState(() {
          // Atribua o texto reconhecido a uma variável
          _recognizedText = val.recognizedWords;
        });
        print(_recognizedText);
      });
    }

    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.teal,
          centerTitle: true,
          title: const Text(
            "Reconhecimento de Voz",
            style: TextStyle(color: Colors.white),
          ),
          // Copiando o texto para "clipboard"
          actions: [
            IconButton(
              onPressed: () async {
                // Colocando o texto na "clipboard"
                await FlutterClipboard.copy(textSample);
                // Fazendo aparecer um "toast"
                ScaffoldMessenger.of(context).showSnackBar(
                  const SnackBar(content: Text('Text Copied to Clipboard')),
                );
              },
              icon: const Icon(
                Icons.copy,
                color: Colors.white,
              ),
            ),
          ],
        ),
        floatingActionButton: AvatarGlow(
          endRadius: 80,
          animate: isListening,
          glowColor: Colors.teal,
          child: FloatingActionButton(
            onPressed: toggleRecording,
            child: Icon(
              isListening ? Icons.circle : Icons.mic,
              size: 35,
            ),
          ),
        ),
        floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
        body: SingleChildScrollView(
          reverse: true,
          child: Padding(
            padding: const EdgeInsets.all(20.0).copyWith(bottom: 140),
            child: SubstringHighlight(
              text: textSample,
              terms: Command.commands,
              textStyle: const TextStyle(
                color: Colors.teal,
                fontSize: 30,
              ),
              textStyleHighlight: const TextStyle(color: Colors.blue, fontSize: 30, fontWeight: FontWeight.bold),
            ),
          ),
        ),
      );
    }

    Future toggleRecording() {
      return Speech.toggleRecording(
        onResult: (String text) {
          setState(() {
            textSample = text;
          });
        },
        onListening: (bool isListening) {
          setState(() {
            this.isListening = isListening;
          });
          if (!isListening) {
            Future.delayed(const Duration(milliseconds: 1000), () {
              Utils.scanVoicedText(textSample);
            });
          }
        },
      );
    }
  }

请忽略“Utils”、“SubstringHighlight”方法

flutter dart speech-recognition speech-to-text
1个回答
0
投票

您需要在监听事件中设置

pauseFor
参数。

pauseFor
设置在未检测到单词的情况下语音暂停的最大持续时间,之后它会自动为您停止收听。 在某些系统上,特别是 Android,系统会强制执行一到三秒的暂停,并且无法覆盖。 该插件确保暂停时间不长于pauseFor值,但也可能更短。

这里是插件提供的示例,您可以在其中看到参数的用法。

© www.soinside.com 2019 - 2024. All rights reserved.