Java服务器,Unity c#客户端冻结

本文关键字:客户端 冻结 Unity 服务器 Java | 更新日期: 2023-09-27 18:13:35

我正在构建一个Sphinx4 Java服务器,Unity3D应该与之通信。从Unity c#发送音频数据到Java服务器工作得很好。语音识别与接收到的数据工作得很好。当我尝试将数据从Java发送回c#时,问题就出现了。

我当前的代码: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;
}
}
现在我的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");
}

识别大约需要10秒。当给出结果时,系统冻结。Println("现在发送")(在Java中)不被打印。所以还没到温度就冻结了。

奇怪的是:当我只从Java发送文本到c#或从c#发送文本到Java时,它可以工作。如果我想在两端发送和接收,它会冻结。我需要同时发送和接收数据

Java服务器,Unity c#客户端冻结

我找到了答案:在RecognizeText中循环不会中断,将return语句置于while循环中可以修复此问题,因为无论如何都会首先返回已识别的文本。