在队列<byte[]>中记录语音并将其发送到服务器 [英] record voice in a Queue&lt;byte[]&gt; and send it to the server

查看:28
本文介绍了在队列<byte[]>中记录语音并将其发送到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发语音应用程序.

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.

这是录音代码:

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

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

推荐答案

当你将一个 byte[] 放入队列时,你需要创建一个新的缓冲区.否则下一个记录将覆盖相同的缓冲区.只需将 bData 的初始化移动到循环中即可:

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 这是记录循环的修改版本,可以进行正确的错误检查.它使用 Queue 而不是 Queue:

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;
        }
    }
}

当然,您需要在某个地方调用 recorder.startRecording() 否则您将无法获得任何数据.

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

这篇关于在队列<byte[]>中记录语音并将其发送到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆