публичный класс SpeakToText реализует RecognitionListener {
Код: Выделить всё
private final String VOSK_TAG = "SpeakRecognizing";
private Context context;
private Model model;
private SpeechService speechService;
private SpeechStreamService speechStreamService;
public void initModel() {
StorageService.unpack(context, "vosk-model-small-fa-0.4", "model",
(model) -> {
this.model = model;
Toast.makeText(context, "Ready", Toast.LENGTH_SHORT).show();
recognizeMicrophone();
},
(exception) -> setErrorState("Failed to unpack the model" + exception.getMessage()));
}
@Override
public void onResult(String hypothesis) {
Log.i("TAG", hypothesis);
}
@Override
public void onFinalResult(String hypothesis) {
if (speechStreamService != null) {
speechStreamService = null;
}
Log.i(VOSK_TAG, "onFinalResult: " + hypothesis);
}
@Override
public void onPartialResult(String hypothesis) {
Log.i(VOSK_TAG, "onPartialResult: " + hypothesis);
}
@Override
public void onError(Exception e) {
setErrorState(e.getMessage());
Log.e(VOSK_TAG, "onError: ", e);
}
@Override
public void onTimeout() {
Log.i(VOSK_TAG, "onTimeout: ");
}
private void setErrorState(String message) {
}
public void recognizeMicrophone() {
if (speechService != null) {
speechService.stop();
speechService = null;
} else {
try {
Recognizer rec = new Recognizer(model, 16000.0f);
speechService = new SpeechService(rec, 16000.0f);
speechService.startListening(this);
} catch (IOException e) {
setErrorState(e.getMessage());
}
}
}
public void pause(boolean checked) {
if (speechService != null) {
speechService.setPause(checked);
}
}
Я хочу, чтобы либо модель помещалась в файлы проекта без ошибок и запускалась, либо файл модели импортировался с мобильного устройства память.Спасибо.
Обновление:
Я вручную скопировал файл модели в нужную папку (\Internal Shared Storage\Android\data\com .example.myapp\files\model). Теперь я не знаю, как импортировать эту модель в приложение.
Подробнее здесь: https://stackoverflow.com/questions/780 ... droid-java