Moin,
da ich gerade mal intensiver mit meinen Tablet rumspiele
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import java.util.Locale;
public class MrSpeaker implements RecognitionListener {
public interface MrSpeakerListener {
void OnWordsRecognized(String word);
}
private final String LOGTAG = "MrSPEAKER";
private SpeechRecognizer speechRecon;
private Intent speechIntent;
private boolean enabled = false;
private MrSpeakerListener listener;
public MrSpeaker(Context context) {
speechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.GERMANY);
if (SpeechRecognizer.isOnDeviceRecognitionAvailable(context)) {
log("Spracherkennung erfolgt auf dem Gerät");
speechRecon = SpeechRecognizer.createOnDeviceSpeechRecognizer(context);
} else {
log("Google (Samsung?) kümmert sich um die Übersetzung");
speechRecon = SpeechRecognizer.createSpeechRecognizer(context);
}
speechRecon.setRecognitionListener(this);
}
public void setListener(MrSpeakerListener listener) {
this.listener = listener;
}
public void setEnable(boolean enabled) {
this.enabled = enabled;
if (enabled) {
speechRecon.startListening(speechIntent);
} else {
speechRecon.stopListening();
}
}
public void destroy() {
speechRecon.stopListening();
speechRecon.destroy();
}
private void log(String msg) {
// Log.i(LOGTAG, "TTS -> " + msg);
}
@Override
public void onReadyForSpeech(Bundle params) {
log("onReadyForSpeech");
}
@Override
public void onBeginningOfSpeech() {
log("onBeginningOfSpeech");
}
@Override public void onRmsChanged(float rmsdB) { }
@Override
public void onBufferReceived(byte[] buffer) {
log("onBufferReceived");
}
@Override
public void onEndOfSpeech() {
log("onEndOfSpeech");
//cleanUpAndRestart();
}
@Override
public void onError(int error) {
log("onError -> " + error);
//cleanUpAndRestart();
if (error == SpeechRecognizer.ERROR_NO_MATCH) {
log("nichts gefunden");
if (listener != null) listener.OnWordsRecognized("nicht erkannt");
if (enabled) speechRecon.startListening(speechIntent);
}
}
@Override
public void onResults(Bundle results) {
for(String word : results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION)) {
if (listener != null) listener.OnWordsRecognized(word);
log("onResults -> " + word);
}
if (enabled) speechRecon.startListening(speechIntent);
}
@Override
public void onPartialResults(Bundle partialResults) {
// for(String word : partialResults.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION)) {
// log("onPartialResults -> " + word);
// if (listener != null) listener.OnWordsRecognized(word);
// }
}
@Override
public void onEvent(int eventType, Bundle params) {
log("onEvent");
}
}
es kann sinnvoll sein, die erkannten Wörter durch Levenshtein laufen zu lassen und etwas Ungenauigkeit zu zulassen.
hand, mogel