找不到文件异常? (语音识别)

File not found exception? (Voice recog)

抱歉这个问题太长了,我已经坚持了一个月,我想提供尽可能多的细节......它只是一个简单库中的文件未找到异常......:)

我的 variances 文件出现找不到文件异常:

不过,我有差异文件:

我正在尝试在我的后台服务中简单地实现语音识别,以便我可以检测到用户何时说出“你好”这个词(使用 pocketsphinx)。

这个方法出现问题:createSphinxDir();

这是我的服务:

 @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;
}

我也有所有回调方法(onPartialResult、onResult 等),但它们从未被调用过。

早些时候我收到一个异常,说差异 .md5 文件不存在,所以我在 variances 和 [=14= 之间放了一个 space ], 但现在我收到这个错误,我不知道为什么...

请告诉我,

鲁奇尔

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...

你不应该做这样的事情,它会导致问题,相反你需要遵循 documentation:

在 Android 中将资源文件与应用程序一起发布的标准方法是将它们放在项目的 assets/ 目录中。但是为了使它们可用于 pocketsphinx 文件应该有物理路径,只要它们在 .apk 之内它们就没有。来自 pocketsphinx-android 的资产 class 提供了一种自动将资产文件复制到目标设备的外部存储的方法。 edu.cmu.pocketsphinx.Assets#syncAssets 从位于 assets/ 顶部的 assets.lst 文件同步资源读取项目。在复制之前,它匹配资产的 MD5 校验和和外部存储上具有相同名称的文件(如果存在)。如果信息不完整(外部存储上没有文件,两个 .md5 文件中没有任何一个)或哈希不匹配,它只会实际复制。 PocketSphinxAndroidDemo 包含生成 assets.lst 以及 .md5 文件的 ant 脚本,寻找 assets.xml.

请注意,如果 ant 构建脚本在您的构建过程中没有 运行 正确,资产可能会不同步。确保脚本 运行s 在构建过程中。

要在您的应用程序中集成资产同步,请执行以下操作

将演示应用程序中的 app/asset.xml 构建文件包含到您的应用程序中。将 build.gradle 构建文件编辑为 运行 assets.xml:

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