Я хочу уметь интегрировать распознавание речи в Codename. Одно приложение при использовании Android, поэтому я решил создать небольшой пример. Вот что я сделал:Простой пример распознавания речи на 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();
В принципе, я хочу, чтобы каждый раз, когда я нажимаю «start», распознавание речи начинает прослушиваться, а когда останавливается - обновлять ярлык с признанным результатом. Затем я создал собственный доступ и в андроиде реализации я использовал следующее:
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, абсолютно ничего не происходит. Кто-нибудь имел успех с этим?
Добавление подсказки сборки android.xpermissions = решил проблему. –