抱歉问了这么长的问题,我已经在这个问题上坚持了一个月了,我想提供尽可能多的细节...这只是一个简单库中未找到文件的异常...:)
我的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
和 .md5
之间放置了一个空格,但现在我收到此错误,我不知道为什么......
请告诉我,
鲁奇
最佳答案
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 中的 Assets 类提供了一种自动将资源文件复制到目标设备外部存储的方法。 edu.cmu.pocketsphinx.Assets#syncAssets
同步从位于顶部 assets/
的 assets.lst
文件读取项目的资源。在复制之前,它会匹配 Assets 和外部存储上同名文件(如果存在)的 MD5 校验和。仅当信息不完整(外部存储上没有文件,两个 .md5
文件中没有任何一个)或存在哈希不匹配时,它才会实际进行复制。 PocketSphinxAndroidDemo 包含生成 assets.lst
以及 .md5
文件的 ant 脚本,查找 assets.xml
。
请注意,如果 ant 构建脚本在构建过程中无法正常运行,资源可能会不同步。确保脚本在构建期间运行。
要将 Assets 同步集成到您的应用程序中,请执行以下操作
将演示应用程序中的 app/asset.xml
构建文件包含到您的应用程序中。编辑 build.gradle
构建文件以运行 assets.xml
:
ant.importBuild 'assets.xml'
preBuild.dependsOn(list, checksum)
clean.dependsOn(clean_assets)
关于java - 文件未找到异常? (语音识别),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35664297/