AndroidStudio中语音后文本不显示问题
创始人
2024-08-21 09:30:09
0
  1. 首先需要在AndroidManifest.xml文件中添加以下权限:




  1. 然后在gradle内添加依赖包:
dependencies {    
    implementation 'com.google.android.gms:play-services-speech:16.0.0'
}
  1. 在布局文件中,添加一个用于接受语音转换后文字的TextView控件,如下所示:

  1. 在Activity或Fragment内定义使用语音输入的方法:
private void startVoiceRecognition() {
    // 创建意图,启动Google语音识别器
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Listening...");

    // 启动意图,等待语音识别结果
    try {
        startActivityForResult(intent, SPEECH_REQUEST_CODE);
    } catch (ActivityNotFoundException e) {
        // 如果设备上未安装语音识别包,则给出提示信息
        Toast.makeText(this, "This device doesn't support speech-to-text", Toast.LENGTH_LONG).show();
    }
}
  1. 在onActivityResult()方法中处理语音识别结果,并将结果显示在TextView中:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     if (requestCode == SPEECH_REQUEST_CODE && resultCode == RESULT_OK) {
         ArrayList matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
         if (matches.size() > 0) {
             String transcript = matches.get(0);
             tvTranscript.setText(transcript);
         }
     }

相关内容

热门资讯

透视好牌!红龙poker透视,... 透视好牌!红龙poker透视,wepoker插件功能辅助器(详细辅助辅助教程)1、红龙poker透视...
微扑克辅助机器人!wepoke... 微扑克辅助机器人!wepoke管理员(透视)切实存在有挂(详细辅助黑科技教程);微扑克辅助机器人!w...
透视神器!智星德州菠萝插件官网... 透视神器!智星德州菠萝插件官网,拱趴大菠萝万能辅助器(详细辅助技巧教程)一、智星德州菠萝插件官网软件...
wepower有外挂!微扑克的... wepower有外挂!微扑克的辅助工具(透视)从前是有挂(详细辅助解密教程)1、金币登录送、破产送、...
透视辅助!wepoker透视脚... 透视辅助!wepoker透视脚本下载,aapoker怎么设置抽水(详细辅助分享教程)1、完成wepo...
wepoke有没有挂!wepo... wepoke有没有挂!wepoke最新下载地址(透视)切实存在有挂(详细辅助力荐教程)wepoke有...
透视中牌率!wpk辅助购买,w... 透视中牌率!wpk辅助购买,wepoker辅助器怎么弄(详细辅助扑克教程);1、实时wpk辅助购买开...
wepoke真的有挂!微扑克必... wepoke真的有挂!微扑克必胜技巧(透视)一贯是有挂(详细辅助技巧教程),您好,wepoke真的有...
透视安装!aapoker辅助挂... 透视安装!aapoker辅助挂,wepoker插件程序激活码(详细辅助软件教程)1、每一步都需要思考...
微扑克辅助机器人!wpk透明挂... 微扑克辅助机器人!wpk透明挂(透视)原来真的是有挂(详细辅助解密教程)1、许多玩家不知道微扑克辅助...