JAVA-未能在swing组件中迭代sphinx4
我正在使用CMU sphinx,我正在运行我的程序,但出现以下错误:“无法打开格式为PCM_签名16000.0 Hz、16位、单声道、2字节/帧、不支持big-endian的麦克风线路。”无法启动麦克风。我想让我的程序在JButton单击时使用JButton收听语音,但我只能在单击单个按钮时执行一次。下一次单击将出现如上所述的错误。请帮我看看有什么问题 这就是我正在做的JAVA-未能在swing组件中迭代sphinx4,java,swing,cmusphinx,sphinx4,Java,Swing,Cmusphinx,Sphinx4,我正在使用CMU sphinx,我正在运行我的程序,但出现以下错误:“无法打开格式为PCM_签名16000.0 Hz、16位、单声道、2字节/帧、不支持big-endian的麦克风线路。”无法启动麦克风。我想让我的程序在JButton单击时使用JButton收听语音,但我只能在单击单个按钮时执行一次。下一次单击将出现如上所述的错误。请帮我看看有什么问题 这就是我正在做的 public class VoiceChat { private static JPanel contentPane; pr
public class VoiceChat {
private static JPanel contentPane;
private static JTextField textField;
private static JTextField textField2;
private static JWindow window;
private static boolean alive = true;
private static String chatMessage;
private static boolean runTimer = true;
private static boolean OverSession = false;
private static Recognizer recognizer;
private static Microphone microphone;
public VoiceChat() {
ExecutorService executor = Executors.newFixedThreadPool(2);
FutureTask<String> CountTimer = new FutureTask<String>(
new ShowCounter());
FutureTask<String> talkMode = new FutureTask<String>(new DoTalk());
executor.submit(CountTimer);
initRecognize();
executor.submit(talkMode);
try {
System.out.println(talkMode.get(20, TimeUnit.SECONDS));
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (TimeoutException e) {
executor.shutdownNow();
e.printStackTrace();
} finally {
executor.shutdown();
}
}
public static void initGUI()
{
window = new JWindow();
window.setBounds(100, 100, 400, 200);
window.setLocationRelativeTo(null);
window.setOpacity(0.9f);
window.setVisible(true);
textField = new JTextField();
textField.setVisible(true);
textField.setEnabled(false);
textField.setBorder(new LineBorder(new Color(139, 0, 139), 2));
textField.setFont(new Font("Tunga", Font.BOLD, 36));
textField.setBounds(138, 83, 100, 53);
window.add(textField);
textField.setColumns(10);
JLabel lblNewLabel = new JLabel("Start Talking in 10 Seconds");
lblNewLabel.setVisible(true);
lblNewLabel.setForeground(new Color(0, 0, 255));
lblNewLabel.setFont(new Font("Batang", Font.PLAIN, 14));
lblNewLabel.setBounds(90, 83, 100, 53);
window.add(lblNewLabel);
}
public static void initRecognize()
{
ConfigurationManager cm = new ConfigurationManager(
VoiceChat.class.getResource("myconfig.xml"));
recognizer = (Recognizer) cm.lookup("recognizer");
recognizer.allocate();
microphone = (Microphone) cm.lookup("microphone");
if (!microphone.startRecording()) {
System.out.println("Cannot start microphone.");
recognizer.deallocate();
microphone.stopRecording();
}
}
public static boolean isAlive() {
return alive;
}
public static void setAlive(boolean alive) {
VoiceChat.alive = alive;
}
public static boolean isRunTimer() {
return runTimer;
}
public static void setRunTimer(boolean runTimer) {
VoiceChat.runTimer = runTimer;
}
public static boolean isOverSession() {
return OverSession;
}
public static void setOverSession(boolean overSession) {
OverSession = overSession;
}
public static String getChatMessage() {
return chatMessage;
}
public static void setChatMessage(String chatMessage) {
VoiceChat.chatMessage = chatMessage;
}
static class ShowCounter implements Callable<String> {
@Override
public String call() throws Exception {
Thread.sleep(5000);
Integer timeNote = 0;
initGUI();
try {
for (int i = 0; i < 10; i++) {
if (!isRunTimer()) {
textField.setVisible(false);
window.setVisible(false);
window.dispose();
break;
}
textField.setText(" " + (10 - i) + "");
timeNote = 10 - (10 - i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
textField.setVisible(false);
window.setVisible(false);
window.dispose();
setAlive(false);
setOverSession(true);
return timeNote.toString();
}
}
static class DoTalk implements Callable<String> {
@Override
public String call() throws Exception {
String resultText = "";
System.out.println("Start speaking. Press Ctrl-C to quit.\n");
while (isAlive()) {
Result result = recognizer.recognize();
if (result != null) {
resultText = result.getBestFinalResultNoFiller();
System.out.println((new StringBuilder())
.append("You said: ").append(resultText)
.append('\n').toString());
if (!resultText.isEmpty()) {
setRunTimer(false);
break;
}
// wordFilter.add(resultText);
} else {
System.out.println("I can't hear what you said.\n");
}
}
microphone.stopRecording();
if (recognizer.getState() == State.ALLOCATED) {
recognizer.deallocate();
}
setChatMessage(resultText);
return resultText;
}
public static void main(String[] args) {
JFrame frames = new JFrame();
frames.setTitle("Simple MultiThread");
frames.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frames.setBounds(100, 100, 450, 300);
frames.setResizable(false);
frames.setVisible(true);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(null);
frames.setContentPane(contentPane);
JButton btnStart = new JButton("Start");
btnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
VoiceChat x = new VoiceChat();
System.out.println(getChatMessage());
}});
btnStart.setFont(new Font("Mangal", Font.BOLD, 16));
btnStart.setBounds(180, 166, 78, 23);
contentPane.add(btnStart);
}
}}
公共类语音聊天{
私有静态JPanel内容窗格;
私有静态JTextField textField;
私有静态JTextField textField2;
私有静态窗口;
私有静态布尔活动=真;
私有静态字符串消息;
私有静态布尔运行计时器=true;
私有静态布尔OverSession=false;
专用静态识别器;
专用静态话筒;
公共语音聊天(){
ExecutorService executor=Executors.newFixedThreadPool(2);
FutureTask CountTimer=新建FutureTask(
新ShowCounter());
FutureTask talkMode=新的FutureTask(new DoTalk());
执行人提交(计数计时器);
initRecognize();
执行人提交(通话模式);
试一试{
System.out.println(talkMode.get(20,TimeUnit.SECONDS));
}捕捉(中断异常e){
e、 printStackTrace();
}捕获(执行例外){
e、 printStackTrace();
}捕获(超时异常e){
执行者。关机现在();
e、 printStackTrace();
}最后{
executor.shutdown();
}
}
公共静态void initGUI()
{
window=新的JWindow();
窗口.立根(100100400200);
window.setLocationRelativeTo(空);
设置不透明度(0.9f);
window.setVisible(true);
textField=新的JTextField();
textField.setVisible(true);
textField.setEnabled(false);
setboorder(新的线条边框(新颜色(139,0139,2));
setFont(新字体(“Tunga”,Font.BOLD,36));
textField.setBounds(138,83100,53);
添加(文本字段);
textField.setColumns(10);
JLabel lblNewLabel=新JLabel(“10秒后开始通话”);
lblNewLabel.setVisible(true);
设置前景(新颜色(0,0,255));
lblNewLabel.setFont(新字体(“Batang”,Font.PLAIN,14));
lblNewLabel.立根(90,83,100,53);
window.add(lblNewLabel);
}
公共静态void initRecognize()
{
ConfigurationManager cm=新的ConfigurationManager(
getResource(“myconfig.xml”);
识别器=(识别器)cm.查找(“识别器”);
recognizer.allocate();
麦克风=(麦克风)厘米查找(“麦克风”);
如果(!micro.startRecording()){
System.out.println(“无法启动麦克风”);
recognizer.deallocate();
麦克风。停止录音();
}
}
publicstaticbooleanisalive(){
活着回来;
}
公共静态void setAlive(布尔活动){
VoiceChat.alive=alive;
}
公共静态布尔isRunTimer(){
返回运行计时器;
}
公共静态void setRunTimer(布尔runTimer){
VoiceChat.runTimer=runTimer;
}
公共静态布尔值isOverSession(){
回归过度;
}
公共静态void setOverSession(布尔型overSession){
过度会话=过度会话;
}
公共静态字符串getChatMessage(){
返回聊天信息;
}
公共静态void setChatMessage(字符串chatMessage){
VoiceChat.chatMessage=chatMessage;
}
静态类ShowCounter实现可调用{
@凌驾
公共字符串调用()引发异常{
睡眠(5000);
整数timeNote=0;
initGUI();
试一试{
对于(int i=0;i<10;i++){
如果(!isRunTimer()){
textField.setVisible(false);
window.setVisible(false);
window.dispose();
打破
}
setText(“+(10-i)+”);
时间注释=10-(10-i);
睡眠(1000);
}
}捕捉(中断异常e){
e、 printStackTrace();
}
textField.setVisible(false);
window.setVisible(false);
window.dispose();
setAlive(假);
设置会话(真);
return timeNote.toString();
}
}
静态类DoTalk实现了可调用{
@凌驾
公共字符串调用()引发异常{
字符串resultText=“”;
System.out.println(“开始说话。按Ctrl-C退出。\n”);
while(isAlive()){
结果=识别器。识别();
如果(结果!=null){
resultText=result.getBestFinalResultNoFiller();
System.out.println((新的StringBuilder())
.append(“你说:”).append(resultText)
.append('\n').toString());
如果(!resultText.isEmpty()){
setRunTimer(假);
打破
}
//添加(resultText);
}否则{
System.out.println(“我听不清你说了什么。\n”);
}
}
麦克风。停止录音();
if(recognizer.getState()==State.ALLOCATED){
recognizer.deallocate();
}
setChatMessage(resultText);
返回结果文本;
}
公共静态void main(字符串[]args){
JFrame frames=新的JFrame();
setTitle(“简单多线程”);
frames.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
框架.立根(100100450300);
frames.setresizeable(false);
frames.setVisible(true);
contentPane=newjpanel();
setboorder(新的EmptyBorder(5,5,5,5));
contentPane.setLayout(null);
frames.setContentPane(contentPane);
JButton btnStart=新JButton(“开始”);