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 mast... 透视解谜!poker master辅助(透视)德州局hhpoker,教程经验(有挂透明挂)-哔哩哔哩...
透视分享!wepoker透视底... 透视分享!wepoker透视底牌(透视)hhpoker辅助挂是真的吗,教程绝活(真实有挂)-哔哩哔哩...
透视专业!wpk有那种辅助吗(... 透视专业!wpk有那种辅助吗(透视)约局吧如何查看是否有挂,教程技法(有挂技术)-哔哩哔哩约局吧如何...
透视透视!wepokerplu... 透视透视!wepokerplus脚本(透视)wepoker透视脚本苹果版,教程操作(了解有挂)-哔哩...
透视透视!hhpoker可以控... 透视透视!hhpoker可以控制牌吗(透视)hardrock透视,教程要领(有挂透视)-哔哩哔哩1、...
透视必备!wepoker轻量版... 透视必备!wepoker轻量版透视(透视)wepoker黑侠辅助器正版下载,教程指南(有挂细节)-哔...
透视揭幕!pokemmo辅助器... 透视揭幕!pokemmo辅助器(透视)wepoker透视挂底牌,教程诀窍(有挂总结)-哔哩哔哩1、进...
透视专业!悦扑克脚本(透视)德... 透视专业!悦扑克脚本(透视)德州局透视脚本,教程教程书(有挂方式)-哔哩哔哩1、玩家可以在德州局透视...
透视总结!wepoker辅助器... 透视总结!wepoker辅助器软件下载(透视)hhpoker有后台操控吗,教程经验(有挂存在)-哔哩...
透视曝光!wepoker免费辅... 透视曝光!wepoker免费辅助器(透视)wpk透视怎么安装,教程窍要(有挂攻略)-哔哩哔哩1、进入...