0

I am trying to record audio into a file in the server side which is an android device and send it to client, another android device when the record is done. It works fine for the first time, but when I record again, my second record is not received by the client. Here is my code

SERVER THREAD:

public class ServerThread implements Runnable {

    public void run() {
        try {
            if (SERVERIP != null) {
                serverSocket = new ServerSocket(SERVERPORT);
                while (true) {
                    client = serverSocket.accept();
                    OutputStream out = client.getOutputStream();
                    while(true){
                        //record status is true 
                        //when record is done
                        if(record_status){
                            try {
                                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File(mFileName)));
                                BufferedOutputStream bout = new BufferedOutputStream(out);
                                copyFile(bis, bout);
                                bout.flush();
                            } catch (FileNotFoundException e) {
                                e.printStackTrace();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            record_status = false;
                        }
                    }
                }
            }
        } catch (Exception e) {

            e.printStackTrace();
        }
    }
}

CLIENT THREAD:

    public class ClientThread implements Runnable {

    public void run() {
        try {
            InetAddress serverAddr = InetAddress.getByName(SERVERIP);
            Socket socket = new Socket(serverAddr, SERVERPORT);
            connected = true;
            while (connected) {
                try {
                    dis = socket.getInputStream();
                    while(true){
                        if(dis.available()>0){
                        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(mFileName)));
                        copyFile(socket.getInputStream(), bos);
                        bos.flush();
                        //file has been received
                        //start playing the audio
                        startPlaying();
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            socket.close();
        } catch (Exception e) {
            connected = false;
        }
    }
}

method copyFile:

    public static boolean copyFile(InputStream inputStream, OutputStream out) {
    byte buf[] = new byte[4092];
    int len;
    try {
        while ((len = inputStream.read(buf)) != -1) {
            out.write(buf, 0, len);

        }
        out.close();
        inputStream.close();
    } catch (IOException e) {

        return false;
    }
    return true;
}
4

1 回答 1

0

这真的应该是一个评论,但是我还没有代表。但在我看来,在服务器端,一旦该流被发送到客户端,您在 while 循环的第一次迭代中将 record_status 设置为 true,然后 record_status 设置为 false,并且在 while 循环内再也没有真正设置为 true。结果, if 语句中的代码永远不会再次执行。那么 record_status 在哪里再次设置为 true 呢?检查服务器端设备上的 logcat 以查看是否有任何内容实际放入比特流中以进行第二次录制,如果不是,这是您的问题

于 2013-03-20T20:54:37.943 回答