我希望能够在使用Android时将语音识别功能集成到Codename One应用程序中,所以我决定创建一个小例子。这是我做的:
我创建了一个新应用,然后我跟着this link,所以我创建了一个本机访问接口:
public interface SpeechRecognition extends NativeInterface {
void startListening();
String getResult();
}
主要表格如下:
SpeechRecognition speechRecognition = NativeLookup.create(SpeechRecognition.class);
Form hi = new Form("Hi World");
Button startButton = new Button("start");
Button stopButton = new Button("stop");
Label label = new Label("non");
startButton.addActionListener(event -> {
if (speechRecognition != null && speechRecognition.isSupported()) {
speechRecognition.startListening();
}
});
stopButton.addActionListener(event -> {
if (speechRecognition != null && speechRecognition.isSupported()) {
label.setText(speechRecognition.getResult());
}
});
hi.addComponent(startButton);
hi.addComponent(stopButton);
hi.addComponent(label);
hi.show();
基本上我希望每当我点击“开始”,语音识别开始收听,停止时 - 用识别结果更新标签。然后我生成了本机访问,在android实现中我使用了以下内容:
public class SpeechRecognitionImpl {
//maybe use a getter for message to get the last event
static String message = "";
static String speech = "";
private SpeechRecognizer sr;
public void startListening() {
//initialize the Intent
final Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
//number of guesses
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);
//Speechrecognition must be run on main Thread
Activity activity = AndroidNativeUtil.getActivity();
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
sr = SpeechRecognizer.createSpeechRecognizer(AndroidNativeUtil.getActivity());
sr.setRecognitionListener(new listener());
sr.startListening(intent);
}
});
}
public String getResult() {
return speech;
}
public boolean isSupported() {
return true;
}
class listener implements RecognitionListener {
public void onReadyForSpeech(Bundle params) {
message = "onReadyForSpeech";
}
public void onBeginningOfSpeech() {
message = "onBeginningOfSpeech";
}
public void onRmsChanged(float rmsdB) {
message = "onRmsChanged";
}
public void onBufferReceived(byte[] buffer) {
message = "onBufferReceived";
}
public void onEndOfSpeech() {
message = "onEndofSpeech";
}
public void onError(int error) {
message = "error " + error;
}
public void onResults(Bundle results) {
//here you have what google understand from the speech
//maybe only save the first guess, which would have the highest
//possibility
speech += "on results";
String str = "";
ArrayList data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
for (int i = 0; i < data.size(); i++) {
str += data.get(i)+";";
}
speech += str;
}
public void onPartialResults(Bundle partialResults) {
message = "onPartialResults";
}
public void onEvent(int eventType, Bundle params) {
message = "onEvent " + eventType;
}
}
这基本上是复制粘贴“应该”工作的例子。但是,当我在我的Android应用程序上运行它时,绝对没有任何反应。有人有成功吗?
答案 0 :(得分:0)
您需要使用DDMS运行应用程序并查看应用程序的日志。我猜你只是错过了使用构建提示添加对清单的权限。