简体   繁体   English

几个小时后,TextToSpeech停止工作

[英]TextToSpeech stops working after a few hours

I'm trying to make an app reading a string coming from a socket connection, but after a few hours the app stops talking (without exceptions). 我正在尝试使一个应用程序读取来自套接字连接的字符串,但是几个小时后,该应用程序停止通话(无例外)。 I'm sure the app is still running because the server sending the string continues to detect the response echo after sending it. 我确定该应用程序仍在运行,因为发送字符串的服务器在发送后继续检测响应回显。

public class MainActivity extends AppCompatActivity {


TextToSpeech textToSpeech;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textToSpeech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            if(status != TextToSpeech.ERROR) {
                textToSpeech.setLanguage(Locale.ITALY);
            }
        }
    });

    Thread socketT = new Thread(new SocketThread());
    socketT.start();

}

@Override
public void onDestroy() {
    if (textToSpeech != null) {
        textToSpeech.stop();
        textToSpeech.shutdown();
    }
    super.onDestroy();
}


private class SocketThread extends Thread {

    static final int socketPort = 3333;

    @Override
    public void run() {
        try {
            ServerSocket serverSocket = new ServerSocket(socketPort);
            try {
                while(true) {
                    Socket clientSocket = serverSocket.accept();
                    try {
                        new ServerThread(clientSocket);
                    } catch(IOException e) {
                        clientSocket.close();
                    } }
            }
            catch (IOException e) {
            }
            serverSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    class ServerThread extends Thread {
        private int counter = 0;
        private int id = ++counter;
        private Socket socket;
        private BufferedReader in;
        private PrintWriter out;
        public ServerThread(Socket s) throws IOException {
            socket = s;
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            OutputStreamWriter osw = new OutputStreamWriter(socket.getOutputStream());
            out = new PrintWriter(new BufferedWriter(osw), true);
            start();
        }
        public void run() {
            try {
                while (true) {
                    String str = in.readLine();
                    textToSpeech.speak(str, TextToSpeech.QUEUE_FLUSH, null);
                }
            } catch (IOException e) {}
            try {
                socket.close();
            } catch(IOException e) {}
        }
    }


}



}

TextToSpeech instance will be available after connect to system service, not after invoke constructor. TextToSpeech实例将在连接到系统服务之后可用,而不是在调用构造函数之后可用。

So, your plan need to edit like this: 因此,您的计划需要像这样进行编辑:

  1. Call TextToSpeech constructor. 调用TextToSpeech构造函数。

  2. Check your TextToSpeech instance is finish to connect to system service through TextToSpeech.OnInitListener.onInit() . 检查您的TextToSpeech实例是否已完成通过TextToSpeech.OnInitListener.onInit()连接到系统服务。

  3. Then, connect to your custom service. 然后,连接到您的自定义服务。

Try this as below: 尝试如下操作:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textToSpeech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            if(status != TextToSpeech.ERROR) {
                int res = textToSpeech.setLanguage(Locale.ITALY);
                if (res >= TextToSpeech.LANG_AVAILABLE) {
                    // TextToSpeech instance is available after connect to system service!
                    Thread socketT = new Thread(new SocketThread());
                    socketT.start();
                }
            }
        }
    });

    // At this time, your TextToSpeech instance may be not available yet.
    //Thread socketT = new Thread(new SocketThread());
    //socketT.start();
}

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

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