简体   繁体   English

Java服务器,Unity C#客户端正在冻结

[英]Java Server, Unity C# Client Are Freezing

I am building a Sphinx4 Java Server where Unity3D should communicate with. 我正在构建一个与Unity3D通信的Sphinx4 Java服务器。 Sending audio data from Unity C# to the Java server works fine. 将音频数据从Unity C#发送到Java服务器工作正常。 Speech recognition with the received data works fine too. 使用接收到的数据进行语音识别也可以正常工作。 The problem appears when I try to send data from Java back to C#. 当我尝试将数据从Java发送回C#时出现问题。

My current code: JAVA 我当前的代码:JAVA

package main;

import java.io.InputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import edu.cmu.sphinx.api.Configuration;
import edu.cmu.sphinx.api.SpeechResult;
import edu.cmu.sphinx.api.StreamSpeechRecognizer;

public class SpeechRecognition {

private static StreamSpeechRecognizer recognizer;
private static Configuration configuration;

public static void main(String[] args)  {

    configuration = new Configuration();
    configuration.setAcousticModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us");
    configuration.setDictionaryPath("resource:/edu/cmu/sphinx/models/en-us/cmudict-en-us.dict");
    configuration.setLanguageModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us.lm.bin");

    try {
        recognizer = new StreamSpeechRecognizer(configuration);
        ServerSocket serverSocket = new ServerSocket(82);

        while (System.in.available() == 0) {
            Socket socket = serverSocket.accept();
            System.out.println("Client found");
            String recognized = RecognizeText(socket.getInputStream());

            System.out.println("sending now");
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

            String json = recognized;
            out.print(json);
            out.flush();
            out.close();

            socket.close();
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    System.out.println("Quitting...");
    serverSocket.close();   

}

private static String RecognizeText(InputStream stream) throws Exception {
    recognizer.startRecognition(stream);
    SpeechResult result;
    String resultString="";
    while ((result = recognizer.getResult()) != null) {
        resultString = result.getHypothesis();
        System.out.format("Hypothesis: %s\n", resultString);
    }
    recognizer.stopRecognition();
    return resultString;
}

}

Now my C# code is like this: 现在我的C#代码是这样的:

void Start () {       
    dataPath = Application.dataPath;
    t = new Thread(Client);
    t.Start();
}

private void Client()
{
    String input;

    TcpClient tcpClient = new TcpClient("localhost", 82);
    NetworkStream networkStream = tcpClient.GetStream();
    BinaryWriter bw = new BinaryWriter(networkStream);

    var filepath = dataPath + "/Resources/audio/test.wav";
    FileStream filestream = new FileStream(filepath, FileMode.Open, FileAccess.Read);
    BinaryReader filereader = new BinaryReader(filestream);
    byte[] bytes = filereader.ReadBytes((Int32)filestream.Length);
    bw.Write(bytes);
    bw.Flush();

    StreamReader streamReader = new StreamReader(networkStream);
    input = streamReader.ReadToEnd();
    print("Received data: " + input + "\n");
}

The recognition takes around 10 seconds. 识别大约需要10秒钟。 When te result is given the system freezes. 当给出结果时,系统冻结。 The Println("sending now") (in Java) is not printed. 不打印Println(“ send now”)(在Java中)。 So it freezes before it even reaches it. 因此它甚至在到达之前就冻结了。

The weird thing is: When I only send text from Java to C# or from C# to Java, it works. 奇怪的是:当我仅将文本从Java发送到C#或从C#发送到Java时,它可以工作。 If I want to send and receive on both ends, it freezes. 如果我想在两端发送和接收,它将冻结。 And I need to send and receive data at the same time 我需要同时发送和接收数据

我发现了答案:在RecognizeText中循环未中断,将return语句放在while循环中可解决此问题,因为无论如何将首先返回识别的文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM