【问题标题】:What does this speech recognition error mean in my flutter app?这个语音识别错误在我的颤振应用程序中意味着什么?
【发布时间】:2021-08-15 22:26:31
【问题描述】:

我的 Flutter 应用程序出现错误,所以基本上我已经对文本应用程序进行了演讲,它可以在我的 iOS 模拟器和设备上完美运行。但是在我的 android 模拟器上,当我启动语音转文本功能时,它会输出此错误:

I/flutter ( 4958): onError: SpeechRecognitionError msg: error_permission, permanent: true

我也在用这个语音转文字包:

speech_to_text: ^2.3.0

有人知道这是什么意思吗?我还在 AndroidManifest.xml、“RECORD_AUDIO”和“INTERNET”中添加了要求。 (这些都列在这个包的 pub.dev 页面上)

这是我的代码:

class SpeechScreen extends StatefulWidget {
  @override
  _SpeechScreenState createState() => _SpeechScreenState();
}

class _SpeechScreenState extends State<SpeechScreen> {
  final Map<String, HighlightedWord> _highlights = {
    'Hablas': HighlightedWord(
      onTap: () => print('voice'),
      textStyle: const TextStyle(
        color: Colors.green,
        fontWeight: FontWeight.bold,
      ),
    ),
  };

  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(
        title: Text('Speech To Text'),
        centerTitle: true,
        backgroundColor: Colors.orange,
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
      floatingActionButton: AvatarGlow(
        animate: _isListening,
        glowColor: Colors.red,
        endRadius: 75.0,
        duration: const Duration(milliseconds: 2000),
        repeatPauseDuration: const Duration(milliseconds: 100),
        repeat: true,
        child: RawMaterialButton(
            onPressed: _listen,
            child: Icon(
              _isListening ? Icons.mic : Icons.mic_none,
              size: 32,
              color: Colors.white,
            ),
            fillColor: Colors.red,
            elevation: 2.0,
            padding: EdgeInsets.all(15.0),
            shape: CircleBorder(),
            constraints: BoxConstraints.tight(Size(64, 64)),
            splashColor: Colors.red),
      ),
      body: SingleChildScrollView(
        reverse: true,
        child: Container(
            padding: const EdgeInsets.fromLTRB(30.0, 30.0, 30.0, 150.0),
            width: double.infinity,
            child: Column(
              children: [
                Text(
                  'AI Confidence Level: ${(_confidence * 100.0).toStringAsFixed(1)}%',
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    fontSize: 32.0,
                    color: Colors.black,
                    fontWeight: FontWeight.w700,
                  ),
                ),
                SizedBox(
                  height: 50,
                ),
                TextHighlight(
                  text: _text,
                  words: _highlights,
                  textAlign: TextAlign.center,
                  textStyle: const TextStyle(
                      fontSize: 32.0,
                      color: Colors.black,
                      fontWeight: FontWeight.w400),
                ),
              ],
            )),
      ),
    );
  }

  void _listen() async {
    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;
            if (val.hasConfidenceRating && val.confidence > 0) {
              _confidence = val.confidence;
            }
          }),
        );
      }
    } else {
      setState(() => _isListening = false);
      _speech.stop();
    }
  }
}

【问题讨论】:

    标签: android flutter gradle speech-recognition speech-to-text


    【解决方案1】:

    它在 android 上不起作用,因为您没有授予它录制音频的权限。导航到此文件:

    <project root>/android/app/src/main/AndroidManifest.xml
    

    并添加这两行

    <uses-permission android:name="android.permission.RECORD_AUDIO"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    

    最后它应该看起来像这样:

    【讨论】:

    • 是的,我这样做了...似乎仍然无法正常工作。
    • 您遇到什么错误?还是一样?如果是,你能把你的清单文件发给我吗? PS:请确保您将其写入主文件夹内的清单文件而不是调试文件夹中。否则它不会工作。
    • 是的,当然,介意我发不和谐吗??
    • 不,只是发送它:)
    猜你喜欢
    • 2019-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-04
    • 1970-01-01
    • 2021-06-10
    相关资源
    最近更新 更多