TextToSpeech几个小时后停止工作

时间:2017-05-22 09:44:22

标签: android sockets text-to-speech

我试图让应用程序读取来自套接字连接的字符串,但几个小时后应用程序停止通话(没有例外)。我确定应用程序仍在运行,因为发送字符串的服务器会在发送后继续检测响应回应。

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) {}
        }
    }


}



}

1 个答案:

答案 0 :(得分:0)

连接到系统服务后,

TextToSpeech实例将可用,而不是在调用构造函数之后。

所以,你的计划需要像这样编辑:

  1. 调用TextToSpeech构造函数。

  2. 检查TextToSpeech实例是否已完成,通过TextToSpeech.OnInitListener.onInit()连接到系统服务。

  3. 然后,连接到您的自定义服务。

  4. 请尝试以下操作:

    @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();
    }