我在Eclipse Helios Java 1.6中工作。我已经下载了CMU Sphinx-4并添加到Java项目中。下面的代码是将音频文件转换为文本文件。
import java.io.File;
import edu.cmu.sphinx.api.Configuration;
import edu.cmu.sphinx.api.SpeechResult;
import edu.cmu.sphinx.api.StreamSpeechRecognizer;
public class AudioToText {
public static void main(String [] args){
Configuration configuration = new Configuration();
// Set path to acoustic model.
configuration.setAcousticModelPath("C:/Program Files/eclipse/sphinx4-5prealpha/models/acoustic");
// Set path to dictionary.
configuration.setDictionaryPath("C:/Program Files/eclipse/sphinx4-5prealpha/models/acoustic/wsj/dict/cmudict.0.6d");
// Set language model.
configuration.setLanguageModelPath("C:/Program Files/eclipse/sphinx4-5prealpha/models/language/en-us.lm.dmp");
StreamSpeechRecognizer recognizer = new StreamSpeechRecognizer(configuration);
recognizer.startRecognition(new File("D:/audio.mp3").toURI().toURL());
SpeechResult result = recognizer.getResult();
recognizer.stopRecognition();
}
}
错误符合:
recognizer.startRecognition(new File("D:/audio.mp3").toURI().toURL());
错误:
- The method startRecognition(InputStream) in the type StreamSpeechRecognizer is not applicable for the
arguments (URL)
- The method startRecognition(InputStream) in the type StreamSpeechRecognizer is not applicable for the
arguments (File)
- Line breakpoint:AudioToText [line: 19] - main(String[])
当我查看startRecognition()定义时,
public void startRecognition(InputStream stream) {
recognizer.allocate();
context.setSpeechSource(stream);
}
如何解决此兼容性问题?代码取自here
从Helios转换到升级版本或Java 1.6升级到更高版本可能会很昂贵,因为很多以前的代码是在Helios和Java 1.6上编写的。
答案 0 :(得分:3)
该方法需要InputStream
作为参数,您无法传递File
或URL
。
只需传入InputStream
:
recognizer.startRecognition(new FileInputStream("D:/audio.mp3"));