简体   繁体   English

android AudioRecord中的BufferSize减小

[英]BufferSize decreases in android AudioRecord

I am trying to live streaming in android to a server. 我正在尝试将android中的流媒体直播到服务器。 My program is working fine. 我的程序运行正常。

The method which starts streaming is: 开始流式传输的方法是:

private void startStreaming() {

    Thread startRecord = new Thread(
            new Runnable() {
                @Override
                public void run() {
                    try {
                        socket = new DatagramSocket();
                    } catch (SocketException e) {
                        e.printStackTrace();
                    }

                    buffer = new byte[minBufSize];

                    DatagramPacket packet;

                    try {
                         destination = InetAddress.getByName("10.31.0.12");
                    } catch (UnknownHostException e) {
                        e.printStackTrace();
                    }

                    record = new AudioRecord(MediaRecorder.AudioSource.MIC,sampleRate,channelConfig,audioFormat,minBufSize * 10);

                    record.startRecording();

                    while ( status )
                    {
                        minBufSize = record.read(buffer,0,buffer.length);
                        packet = new DatagramPacket(buffer,buffer.length,destination,port);
                        Log.d(LOG,"Writing in status " + destination );
                        try {
                            Log.d(LOG, "Trying to send. " + minBufSize);
                            socket.send(packet);
                            Log.d(LOG, "Sent successfully.");
                        } catch (IOException e) {

                            Log.d(LOG,"Socket is not sending properly.");
                            e.printStackTrace();
                        }
                    }

                }
            }
    );
    startRecord.start();
}

The method which stops recording : 停止记录的方法:

public void stopRecording(View view) {
    start.setVisibility(View.VISIBLE);
    stop.setVisibility(View.GONE);
    status = false;
    Log.d(LOG,"I am about to release the record.");
    record.release();
    Log.d(LOG, "I have released.");
}

I am getting minBufSize using 我正在使用minBufSize

 minBufSize = record.read(buffer,0,buffer.length);

If I keep stopping and playing recording, the minBufSize keeps on decreasing. 如果我继续停止并播放录音,则minBufSize会继续减小。 Values changes from 1280 to 512 to 256 to -3 and finally I am getting : 值从1280变为512,从256变为-3,最后我得到:

java.lang.NegativeArraySizeException: -3 at java.lang.NegativeArraySizeException:-3在
com.example.khan.safevoicerecorder.MainActivity$1.run(MainActivity.java:81) com.example.khan.safevoicerecorder.MainActivity $ 1.run(MainActivity.java:81)

Why is this happening? 为什么会这样呢? Please help me to solve this. 请帮我解决这个问题。

You create buffer using the current minBufSize . 您可以使用当前的minBufSize创建buffer Then, your loop reads for a while: minBufSize = record.read(buffer,0,buffer.length) . 然后,循环读取一会儿: minBufSize = record.read(buffer,0,buffer.length) Since the read will be at most buffer.length bytes, you may now have decreased minBufSize . 由于读取的内容最多为buffer.length字节,因此您现在可以减小minBufSize

The next time you start a recording, your buffer will be created with the new, smaller minBufSize . 下次开始记录时,将使用较小的新minBufSize创建缓冲区。 Again, the read will be at most buffer.length bytes (which is now smaller than the last time). 再次,读取将最多为buffer.length字节(现在小于上一次)。

Rinse and repeat -- then eventually, you'll get a very small buffer. 冲洗并重复-最终,您将获得一个非常小的缓冲区。 I'm guessing that gives you the -3 error value. 我猜这给你-3错误值。 And finally, when minBufSize is -3 , your buffer allocation will fail; 最后,当minBufSize-3 ,您的buffer分配将失败。 an array can't have a negative size. 数组不能为负数。

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

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