我这样做了吗?

Lit*_*ild 10 java audio byte bytebuffer short

在此输入图像描述



在我寻求帮助之前,让我告诉你我做了什么:假设我的采样率为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
Run Code Online (Sandbox Code Playgroud)

循环并随后将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++;
    }  
Run Code Online (Sandbox Code Playgroud)

问题

  • 即使buffer.position()在收益16000 if声明,我得到一个BufferUnderflowException当我做buffer.asShortBuffer.get(audioSamples);

  • java.util.Arrays.toString()以前看到了我的eachPassaudioSamples包含的内容,我在每个通道中得到了一些数字,如-107,0,32等,以及audioSamples中的所有零.为什么?
  • 退伍军人,请你帮我修这个代码?我不知道发生了什么.

    Ant*_*oly 7

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

    buffer.flip();
    buffer.asShortBuffer.get(audioSamples);
    
    Run Code Online (Sandbox Code Playgroud)