10

在此处输入图像描述



在我寻求帮助之前,让我告诉你我做了什么:假设我的采样率为 8000Hz样本大小为 16 位(2 字节),在第二秒结束时,我需要 16000 字节或 8000 字节。
现在我有一个10fps的记录速度,然后对于每个 fps 我需要 16000/10 = 1600 字节。
所以,故事是这样发展的:

声明的变量:

byte[] eachPass = new byte[1600]; //used to store data from TargetDataLine for each pass
byte[] backingArray = new byte[16000]; //the complete data for one second
ByteBuffer buffer = ByteBuffer.wrap(backingArray); //buffer which stores the complete data
short[] audioSample = new short[16000/2]; //audio samples to be encoded
int passCounter = 0; /* After 10th pass, convert the byte[] to short[]
                      * using ByteBuffer */
int seconds = 0; // used to store the position of the packet

byte[] 到 short[] 的循环和后续转换

while(keepCapturing == true){
    -- set up the java.awt.Robot and TargetDataLine before entering the loop --
    -- use java.awt.Robot to record the screen --
    -- do some other stuff, if needed --
    fromMic.read(eachPass,0,eachPass.length); // read data from microphone
    buffer.put(eachPass); //put it in  a bigger buffer

    if(passCounter!=0 && passCounter%10==0){ // is it 10th frame?
        passCounter = 0; //reset counter
        seconds++;
        buffer.asShortBuffer.get(audioSamples); //get short[] in BigEndian format
        -- encode the audio at position (seconds-1) --
        buffer.clear();
    }else{
        passCounter++;
    }  

问题

  • 即使在语句buffer.position()中返回 16000 ,我也会得到一个ifBufferUnderflowExceptionbuffer.asShortBuffer.get(audioSamples);

  • 我曾经java.util.Arrays.toString()看到我的eachPassaudioSamples包含的内容,我在 eachPass 中得到了一些数字,例如 -107、0、32 等,在 audioSamples 中得到了全零。为什么?
  • 退伍军人,请您帮我确定此代码吗?我不知道发生了什么事。

    4

    1 回答 1

    7

    您忘记在读取数据之前翻转缓冲区,这就是为什么没有写入audioSamples.

    buffer.flip();
    buffer.asShortBuffer.get(audioSamples);
    
    于 2012-12-26T19:18:47.620 回答