简体   繁体   中英

record voice in a Queue<byte[]> and send it to the server

I am developing voice application.

I need a buffer queue of some sort so that i record continuosly in a thread , place the buffers full of bytes into the queue and to transmit to the server, and i take the next buffer from the queue.

Here is the recording code:

     Queue<byte[]> qArray = new LinkedList<byte[]>();
     recordingThread = new Thread(new Runnable() {

        @Override
        public void run() {

            bData = new byte[BufferElements];

            while (isRecording) {
                recorder.read(bData, 0, BufferElements);
                qArray.add(bData);
                if (AudioRecord.ERROR_INVALID_OPERATION != 0) {
                    SendAudio();

                }

            }
        }
    }, "AudioRecorder Thread");
    recordingThread.start();

But still its missing few of byte[] data while sending it to the server

Here is the sending voice to the server code:

           try {
            HttpClient httpclient = new DefaultHttpClient();


            HttpPost httppost = new HttpPost(ServerUrl.url_audio);

            // Json Format
            JSONObject holder = new JSONObject();
            JSONArray jArray = new JSONArray();
            try {

                byte[] tmparr = qArray.poll();
                for (int i = 0; i < tmparr.length; i++) {
                    jArray.put(i, tmparr[i]);
                }

                holder.put("Voice", jArray);

I dont want to miss any data which is recording.

Any help would be appreciated lot. Thanks

When you place a byte[] into the queue, you need to then create a new buffer. Otherwise the next recording will overwrite the same buffer. Just move the initialization of bData into the loop:

Queue<byte[]> qArray = new LinkedList<byte[]>();
recordingThread = new Thread(new Runnable() {

    @Override
    public void run() {
        while (isRecording) {
            bData = new byte[BufferElements];
            recorder.read(bData, 0, BufferElements);
            qArray.add(bData);
            if (AudioRecord.ERROR_INVALID_OPERATION != 0) {
                SendAudio();
            }
        }
    }
}, "AudioRecorder Thread");
recordingThread.start();

You should also add logic to limit the size of the queue. If the queue overflows, you will still lose data, but at least you won't crash with an out-of-memory error.

EDIT Here's a modified version of the recording loop that does proper error checking. It uses a Queue<ByteBuffer> instead of a Queue<byte[]> :

public void run() {
    bData = ByteBuffer.allocate(BufferElements);
    while (isRecording && !isInterrupted()) {
        int result = recorder.read(bData, 0, BufferElements);
        if (result > 0) {
            qArray.add(bData);
            SendAudio();
            bData = ByteBuffer.allocate(BufferElements);
        } else if (result == AudioRecord.ERROR_INVALID_OPERATION) {
            Log.e("Recording", "Invalid operation error");
            break;
        } else if (result == AudioRecord.ERROR_BAD_VALUE) {
            Log.e("Recording", "Bad value error");
            break;
        } else if (result == AudioRecord.ERROR) {
            Log.e("Recording", "Unknown error");
            break;
        }
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            break;
        }
    }
}

Of course, somewhere you'll need to call recorder.startRecording() or you won't get any data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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