android - 这个语音识别错误在我的 flutter 应用程序中意味着什么?

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

我的 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,因为您没有授予它录制音频的权限。导航到此文件:

<project root>/android/app/src/main/AndroidManifest.xml
并将这两行添加到它
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.INTERNET"/>
最后它应该看起来像这样:
enter image description here

关于android - 这个语音识别错误在我的 flutter 应用程序中意味着什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67725879/

相关文章:

android - 如何创建一个根据变化的变量显示其状态的复选框?

android - 使用 ViewModel 的接口(interface)?

android - Kotlinx 综合和 AndroidX

android - Facebook 登录 CallbackManager FacebookCallback 每次都调用 onCancel()

gradle - 如何将检查样式添加到libGDX项目?

java - Android Studio 无法解决错误 ':app:preDexDebug'

flutter - 如何将文本字段的标签转移到前缀图标的顶部而不是文本

flutter - 如何在 flutter 中添加整数作为 TextFormField 的默认值/初始值?

flutter - 如何清理 flutter 过时的 pubs

安卓 gradle 构建 : Share library between parent and child