gpt4 book ai didi

java - Android 上的简单语音识别应用程序无法按预期工作

转载 作者:太空宇宙 更新时间:2023-11-04 11:32:33 27 4
gpt4 key购买 nike

我编写的一个简单的语音识别应用程序无法按我的预期工作。基本上,日志上没有明显的错误,并且编译得很好,但是当我单击按钮时,语音识别功能会弹出并开始录制,但录制完成后,应用程序屏幕上不会出现任何内容, ListView 应该显示录制的内容(以文本格式)

这是java代码

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;


public class Voice extends Activity implements View.OnClickListener {

ListView lv;
final static int check = 1111;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.voice);
lv = (ListView) findViewById(R.id.lvVoiceReturn);
Button b = (Button) findViewById(R.id.bVoice);
b.setOnClickListener(this);
}



@Override
public void onClick(View v) {
Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); // set speech recognizer intent
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); //set language
i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak up son!"); // set prompt to user
startActivityForResult(i, check);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == check && resultCode == RESULT_OK) {
// create an empty array list and link it to the recognizer intent
ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
// link the listview from my layout to the arraylist I created just now with the results from voice recognition
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results));
}

}
}

这是 xml 布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:id="@+id/bVoice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Click to Speak" />

<ListView
android:id="@+id/lvVoiceReturn"
android:layout_width="match_parent"
android:layout_height="fill_parent" />
</LinearLayout>

谢谢!

最佳答案

@Kalid 你可以尝试将语言设置为intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");并使用调试或日志检查应用程序控件是否转到 onActivityResult

关于java - Android 上的简单语音识别应用程序无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43640052/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com