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);
         }
     }

相关内容

热门资讯

一分钟带你了解!hhpoker... 一分钟带你了解!hhpoker可以开挂吗,wepoker祈福有用吗,都是真的是有挂(有挂秘笈)在进入...
分享给玩家!wepoker辅助... 分享给玩家!wepoker辅助透视软件,wepoker挂,好像真的有挂(竟然有挂)1、分享给玩家!w...
揭秘关于!pokemmo手机脚... 揭秘关于!pokemmo手机脚本,wepoker辅助器激活码,好像真的有挂(了解有挂)1、金币登录送...
玩家亲测!wepoker有人用... 玩家亲测!wepoker有人用过吗,pokerrrr2辅助,竟然存在有挂(有挂技巧)1、玩家可以在线...
科普常识!poker worl... 科普常识!poker world辅助器,哈糖大菠萝攻略,原来是有挂(有挂技巧)1、任何透视是真的假的...
传递经验!pokemmo手机脚... 传递经验!pokemmo手机脚本辅助器,来玩德州破解器,原来是有挂(有挂方略)1、完成有辅助插件,帮...
一分钟揭秘!wepoker辅助... 一分钟揭秘!wepoker辅助透视软件,大菠萝789辅助器下载,总是真的有挂(有挂攻略)1、用户打开...
盘点一款!wepoker祈福有... 盘点一款!wepoker祈福有用吗,werplan外挂,其实有挂(真是有挂)1、操作简单,无需手机版...
分享一款!wepokerplu... 分享一款!wepokerplus作弊,hhpoker德州机器人,本来有挂(有挂秘籍)1、该软件可以轻...
玩家必看攻略!hhpoker有... 玩家必看攻略!hhpoker有后台操控吗,wepoker透视最简单三个步骤,原来存在有挂(有挂头条)...