File not found exception? (Voice recog)

淺唱寂寞╮ 提交于 2019-12-12 01:45:21

问题


Sorry for the long question, I have been stuck on this for a month, and I want to provide as much detail as possible...its just a file not found exception in a simple library... :)

I am getting a file not found exception on my variances file:

I do, however, have the variances file:

I am trying to simply implement voice recognition in my background service, so that I can detect when the user says the word hello (using pocketsphinx).

The problem happens in this method: createSphinxDir();

Here is my service:

 @Override
    public void onCreate() {
        super.onCreate();
       setupRecog();

        }
     private void setupRecog() {
    String sphinxDir = createSphinxDir();
    Log.v(TAG, "ABOUT TO CREATE SETUP");
    if (sphinxDir != null) {
        try {

            Log.v(TAG, "SETTING UP! :)");
            mSpeechRecognizer = defaultSetup()
                    .setAcousticModel(new File(sphinxDir, "en-us-ptm"))
                    .setDictionary(new File(sphinxDir, "hello.dict"))
                    .setBoolean("-allphone_ci", true) //WHAT IS THIS
                    .getRecognizer();
            mSpeechRecognizer.addListener(this);

            Log.v(TAG, "ADDED LISTENER");

            if ((new File(sphinxDir + File.separator + "command.gram")).isFile()) {
                mSpeechRecognizer.addKeywordSearch("hello",
                        new File(sphinxDir + File.separator + "command.gram"));

                Log.v(TAG, "ADDED KEYWORD SEARCH! :)");
            }

            // Or wherever appropriate
            mSpeechRecognizer.startListening("wakeup"); //Is this correct?
            Log.v(TAG, "STARTED LISTENING");

        } catch (IOException e) {

            Log.v("ERROR", TAG);

        }
    }
}


String createSphinxDir() {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    String sphinxDir = prefs.getString("sphinx", null);
    if (sphinxDir == null) {
        Assets assets;
        Log.v(TAG, "Assets are not synced, should sync now:");
        try {
            Log.v(TAG, "In try block!");
            assets = new Assets(this);
            File sphinxDirFile = assets.syncAssets();
            Log.v(TAG, "Syncing assets...should set up listener");
            if (sphinxDirFile != null) {
                sphinxDir = sphinxDirFile.getAbsolutePath();
                SharedPreferences.Editor editor = prefs.edit();
                editor.putString("sphinx", sphinxDir);
                editor.commit();
                Log.v(TAG, "Set up listener");

            }else{
                Log.v(TAG, "sphinxDirFile is null!");
            }
        } catch (IOException e) { //THIS IS THE PLACE WHERE I AM GETTING THE ERROR!
            e.printStackTrace();
            Log.d(TAG, e.toString());
        }
    }
    return sphinxDir;
}

I also have all the call back methods (onPartialResult, onResult, etc.) but they never get called.

Earlier I was getting an exception saying the variances .md5 file didn't exist, so I put a space in between the variances and the .md5, but now I am getting this error, and I don't know why...

Please let me know,

Ruchir


回答1:


Earlier I was getting an exception saying the variances .md5 file didn't exist, so I put a space in between the variances and the .md5, but now I am getting this error, and I don't know why...

You should not do such things, it causes problems, instead you need to follow the documentation:

The standard way to ship resource files with your application in Android is to put them in assets/ directory of your project. But in order to make them available for pocketsphinx files should have physical path, as long as they are within .apk they don't have one. Assets class from pocketsphinx-android provides a method to automatically copy asset files to external storage of the target device. edu.cmu.pocketsphinx.Assets#syncAssets synchronizes resources reading items from assets.lst file located on the top assets/. Before copying it matches MD5 checksums of an asset and a file on external storage with the same name if such exists. It only does actualy copying if there is incomplete information (no file on external storage, no any of two .md5 files) or there is hash mismatch. PocketSphinxAndroidDemo contains ant script that generates assets.lst as well as .md5 files, look for assets.xml.

Please note that if ant build script doesn't run properly in your build process, assets might be out of sync. Make sure that script runs during the build.

To integrate assets sync in your application do the following

Include app/asset.xml build file from the demo application into your application. Edit build.gradle build file to run assets.xml:

  ant.importBuild 'assets.xml'
  preBuild.dependsOn(list, checksum)
  clean.dependsOn(clean_assets)


来源:https://stackoverflow.com/questions/35664297/file-not-found-exception-voice-recog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!