sarah - 2017-04-19

I want to create simple speech recognizer using pocket sphinx for my android application such that as soon as activity launches, when I'm running my app its crashing with this logcat:

basically An error occured while executing doInBackground(), Caused by: java.lang.RuntimeException: new_Decoder returned -1
at edu.cmu.pocketsphinx.PocketSphinxJNI.new_Decoder__SWIG_1(Native Method)

What I'm trying

  private static final String NGRAM_SEARCH = "test" ;
    private SpeechRecognizer recognizer;
    TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.tv);

        recognitionSetup();
    }

    private void recognitionSetup() {

        new AsyncTask<Void, Void, Exception>() {
            @Override
            protected Exception doInBackground(Void... params) {
                try {
                    Assets assets = new Assets(MainActivity.this);
                    File assetDir = assets.syncAssets();
                    runRecognizer(assetDir);
                } catch (IOException e) {
                    return e;
                }
                return null;
            }

            @Override
            protected void onPostExecute(Exception e) {
                if (e != null) {
                    Log.i("MainActivity", "Failed to init recognizer ");
                } else {
                    //  switchSearch(KWS_SEARCH);
                    recognizer.startListening(NGRAM_SEARCH);
                }
            }
        }.execute();

    }

    private void runRecognizer(File assetsDir) throws IOException {

        recognizer = SpeechRecognizerSetup.defaultSetup()
                .setAcousticModel(new File(assetsDir, "files"))
                .setDictionary(new File(assetsDir, "model.dic"))
                        //.setRawLogDir(assetsDir)// To disable logging of raw audio comment out this call (takes a lot of space on the device)
                .setBoolean("-allphone_ci", true)
                .getRecognizer();
        recognizer.addListener(this);
       // recognizer.addGrammarSearch(TEST, new File(assetsDir, "Grammar.jsgf"));
        recognizer.addNgramSearch(NGRAM_SEARCH, new File(assetsDir, "en-us.lm.bin"));;

    }

    @Override
    public void onBeginningOfSpeech() {
        Log.i("MainActivity", "onBeginningOfSpeech");

    }

    @Override
    public void onEndOfSpeech() {

            switchSearch(NGRAM_SEARCH);
        Log.i("MainActivity ", "onEndOfSpeech");

    }

    @Override
    public void onPartialResult(Hypothesis hypothesis) {

        if (hypothesis == null)
            return;

        String text = hypothesis.getHypstr();
        if (text.contains(NGRAM_SEARCH)) {
            Toast.makeText(this, "onPartialResult text=" + text, Toast.LENGTH_SHORT).show();
            switchSearch(NGRAM_SEARCH);

        }

        Log.i("MainActivity ", "onPartialResult text=" + text);

    }

    private void switchSearch(String ngramSearch) {
        if (recognizer != null) {
            Log.i("RecognizerNull ", "Null");
            recognizer.stop();
            recognizer.startListening(ngramSearch);
            new java.util.Timer().schedule(
                    new java.util.TimerTask() {
                        @Override
                        public void run() {
                            recognizer.startListening(NGRAM_SEARCH);
                        }
                    },
                    1000
            );

        }
    }

    @Override
    public void onResult(Hypothesis hypothesis) {
        if (hypothesis != null) {
            String text = hypothesis.getHypstr();
            Log.i("MainActivity", "onResult text=" + text);
            tv.setText(text);

        }

    }

    @Override
    public void onError(Exception e) {

        Log.i("MainActivity", "onError " + e.getMessage());

    }

    @Override
    public void onTimeout() {
        switchSearch(NGRAM_SEARCH);
        Log.i("MainActivity ", "onTimeout");

    }