flutter 语音转文本插件自动停止捕获音频

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

我使用语音转文本小部件在 flutter 中实现了语音转文本服务。它基本上会监听用户所说的内容,当他说句号时,它应该将捕获的句子发送到 Flask 后端。下面是代码,

class _AudioInputViewState extends State<AudioInputView> {
  late UnityWidgetController _unityWidgetController;
  String sentence1 ="";
  String sentence2 ="";
  List<String> sentences=[];
  List<String> sentenceTokens=[];
  int starterIndex=0;
  final Map<String, HighlightedWord> _highlights = {
    'period': HighlightedWord(
      onTap: () => print('period'),
      textStyle: const TextStyle(
        color: Colors.yellow,
        fontWeight: FontWeight.bold,
      ),
    ),
  };
  late stt.SpeechToText _speech;
  bool _isListening = false;
  String _text = 'Press the button and start speaking';
  double _confidence = 1.0;

  @override
  void initState() {
    super.initState();
    _speech = stt.SpeechToText();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(

      ),
      body:
      Container(
        decoration: BoxDecoration(
          gradient: LinearGradient(
            begin: Alignment.topLeft,
            end: Alignment.bottomRight,
            colors: [Colors.blue.shade700, Colors.green.shade700],
          ),
        ),
        child: SafeArea(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text(
                "Translator App",
                style: TextStyle(
                  fontSize: 28,
                  fontWeight: FontWeight.bold,
                  color: Colors.white,
                ),
              ),
              SizedBox(height: 30),
              SingleChildScrollView(
                reverse: true,
                child: Container(
                  padding: const EdgeInsets.fromLTRB(30.0, 30.0, 30.0, 150.0),
                  child: TextHighlight(
                    text: _text,
                    words: _highlights,
                    textStyle: const TextStyle(
                      fontSize: 32.0,
                      color: Colors.black,
                      fontWeight: FontWeight.w400,
                    ),
                  ),
                ),
              ),
              SizedBox(height: 30),
              AvatarGlow(
                animate: _isListening,
                glowColor: Theme
                    .of(context)
                    .primaryColor,
                endRadius: 75.0,
                duration: const Duration(milliseconds: 500),
                repeatPauseDuration: Duration(milliseconds: 500),
                repeat: true,
                child: FloatingActionButton(
                  onPressed: _listen,
                  child: Icon(
                    _isListening ? Icons.mic : Icons.mic_none,
                    size: 32,
                  ),
                  backgroundColor: Colors.white,
                  foregroundColor: Colors.black,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
  void _listen() async {
    final TranslatorProvider translatorProvider =
    Provider.of<TranslatorProvider>(context, listen: false);
    if (!_isListening) {
      bool available = await _speech.initialize(
        onStatus: (val) => print('onStatus: $val'),
        onError: (val) => print('onError: $val'),
      );
      if (available) {
        setState(() => _isListening = true);
        _speech.listen(
          onResult: (val) {
            setState(() {
              _text = val.recognizedWords;
              sentenceTokens.clear();
              List<String> words = _text.split(" ");
              for (int i = starterIndex; i < words.length; i++) {
                if (words[i] == "period") {
                  String sentence = sentenceTokens.join(' ');
                  sentences.add(sentence);
                  sentenceTokens.clear();
                  print(sentences);
                  print("---------------------------");
                  print(sentences.last);
                  starterIndex = i + 1;
                  translatorProvider.sendText(sentences.last).then((_) {
                    logger.d("TEXT SENT : ${sentences.last}");
                  }).catchError((e) {
                    logger.e(e);
                  });
                  break;
                } else {
                  sentenceTokens.add(words[i]);
                }
              }
              if (val.hasConfidenceRating && val.confidence > 0) {
                _confidence = val.confidence;
              }
            });
          },
          partialResults: true, // enable partial results
        );
      }
    } else {
      setState(() => _isListening = false);
      _speech.stop();
      print('Listened text: $_text');
    }
  }



}

问题在于,即使处于 isListening 状态,一段时间后应用程序也会停止捕获音频。特别是在短暂的停顿或沉默之后。这在这个插件中是正常的吗?或者我可以做些什么让应用程序即使在静音后也能持续收听音频?

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

这是语音转文本库的预期功能。

官方文档是这样规定的:

'连续语音识别 关于如何使用此插件实现连续语音识别存在许多问题。目前,该插件设计用于短暂间歇性使用,例如在等待对问题的答复或发出单个语音命令时。'

但是,您可以使用pauseFor。

'[pauseFor] 设置在未检测到单词的情况下语音暂停的最长持续时间,之后它会自动为您停止收听。'

请记住,某些设备有内置的暂停限制。因此,即使在达到您设定的时间限制之前,它也可能会暂停。

_speech.listen(
    pauseFor: const Duration(seconds: 10),

您可以根据自己的喜好更改持续时间。

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