我创建了一个java应用程序,它在我的java的src文件夹中包含以下目录结构.
Src/ Transcriber.java config.xml digits.gram transcriber.manifest
我已经使用.aar文件成功创建了这个java应用程序的web服务并放入了axis2服务文件夹
我将这整个打包在Transcriber.aar文件中,具有以下结构
Transcriber\edu\cmu\sphinx\demo\transcriber
在我列出的所有4个文件中.
我在上面的Transcriber.java类中有两个方法.第一个方法只是处理而没有任何其他文件使用(例如config.xml,digits.gram和transcriber.manifest).它的工作正常,我可以从android轻松调用该方法.
但我的第二种方法也使用其他文件(例如config.xml,digits.gram和transcriber.manifest)来处理我想要的逻辑.
但是当我从android设备调用第二种方法时,当我调用第二种方法并且它给我错误时,我会收到错误.
我的错误如下:
at java.lang.Thread.run(Thread.java:662) Caused by: Property exception component:'jsgfGrammar' property:'grammarLocation' - Can't locate resource:/edu/cmu/sphinx/demo/transcriber edu.cmu.sphinx.util.props.InternalConfigurationException: Can't locate resource: /edu/cmu/sphinx/demo/transcriber
它给我一些错误,一些如何找不到语法文件digits.gram我用来通过config.xml文件添加这个代码在config.xml中
<component name="jsgfGrammar" type="edu.cmu.sphinx.jsgf.JSGFGrammar"> <property name="dictionary" value="dictionary"/> <property name="grammarLocation" value="resource:/edu/cmu/sphinx/demo/transcriber"/> <property name="grammarName" value="digits"/> <property name="logMath" value="logMath"/> </component>
为什么我有这种错误?在这里输入代码
我的代码我首先获得CONFIG.XML,然后CONFIG.XML获取另一个资源文件….它成功找到config.xml但是然后config.xml中的代码无法找到其他资源文件
package edu.cmu.sphinx.demo.transcriber; import edu.cmu.sphinx.frontend.util.AudioFileDataSource; import edu.cmu.sphinx.recognizer.Recognizer; import edu.cmu.sphinx.result.Result; import edu.cmu.sphinx.util.props.ConfigurationManager; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; /** A simple example that shows how to transcribe a continuous audio file that has multiple utterances in it. */ public class TranscribeSimpleGrammar { private static final String PATH = "file:///D:\\Sound\\"; @SuppressWarnings({ "null", "null" }) public String recognize_wave(String wavePath) throws MalformedURLException{ URL audioURL; // if (args.length > 0) { // audioURL = new File(args[0]).toURI().toURL(); // } else { //audioURL = TranscribeSimpleGrammar.class.getResource("hello.wav"); //audioURL = new URL(PATH + "turn-on-light-kitchen-male.wav"); //audioURL = new URL(PATH + "turn-down-tv-volume-female.wav"); // audioURL = new URL(PATH + wavePath); audioURL = new URL(wavePath); //audioURL = new URL(PATH + "turn-down-dining-room-music-player-volume-male.wav"); // } URL configURL = TranscribeSimpleGrammar.class.getResource("config.xml"); ConfigurationManager cm = new ConfigurationManager(configURL); Recognizer recognizer = (Recognizer) cm.lookup("recognizer"); /* allocate the resource necessary for the recognizer */ recognizer.allocate(); // configure the audio input for the recognizer AudioFileDataSource dataSource = (AudioFileDataSource) cm.lookup("audioFileDataSource"); dataSource.setAudioFile(audioURL, null); // Loop until last utterance in the audio file has been decoded, in which case the recognizer will return null. Result result; while ((result = recognizer.recognize())!= null) { String resultText = result.getBestResultNoFiller(); System.out.println(resultText); } return result.getBestResultNoFiller(); } public String get_wav_byte(byte[] wavbite,String path){ String result1="null"; try { File dstFile = new File(path); FileOutputStream out = new FileOutputStream(dstFile); out.write(wavbite, 0, wavbite.length); out.close(); } catch (IOException e) { System.out.println("IOException : " + e); } try { result1=recognize_wave(path); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return result1; } }你有没有试过改变
<property name="grammarLocation" value="resource:/edu/cmu/sphinx/demo/transcriber"/>
至
<property name="grammarLocation" value="resource:edu/cmu/sphinx/demo/transcriber"/>
(意思是在edu之前删除前导斜杠)?
Class.getResource()和ClassLoader.getResource()会以不同的方式解释提供的名称:当Class.getResource(“/edu/cmu/sphinx/demo/transcriber/transcriber.manifest”)找到资源时,您必须使用ClassLoader .getResource()使用参数“edu / cmu / sphinx / demo / transcriber / transcriber.manifest”来查找相同的资源.由于您不知道您调用的库代码使用哪种方法来加载其他资源,因此您应该尝试一下.