简体   繁体   English

在队列中录制语音<byte[]>并将其发送到服务器

[英]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但是在将它发送到服务器时仍然缺少一些 byte[] 数据

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.当您将 byte[] 放入队列时,您需要创建一个新缓冲区。 Otherwise the next recording will overwrite the same buffer.否则下一个记录将覆盖相同的缓冲区。 Just move the initialization of bData into the loop:只需将bData的初始化移动到循环中:

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[]> :它使用Queue<ByteBuffer>而不是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.当然,在某个地方您需要调用recorder.startRecording()否则您将无法获得任何数据。

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

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