加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 我正在做这个吗?

发布时间:2020-12-14 19:28:36 所属栏目:Java 来源:网络整理
导读:在我要求帮助之前,让我告诉你我做了什么: 假设我的采样率为8000Hz,采样大小为16位(2字节),则在第二个采样周期,我需要16000字节或8000短. 现在我有一个10fps的录制速度,那么对于每个fps我需要16000/10 = 1600字节. 所以,这是故事如何进行: 变量声明: byte[
在我要求帮助之前,让我告诉你我做了什么:
假设我的采样率为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

字节[]到循环和随后的转换[]

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,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()在if语句中返回16000,当我做buffer.asShortBuffer.get(audioSamples)时,我得到一个BufferUnderflowException;
>我使用java.util.Arrays.toString()来查看我的eachPassand audioSamples包含什么,我在每个Pass中有一些数字,例如-107,32等,并且audioSamples中的所有零.为什么?

退伍军人,你能帮我指点这个代码吗?我不知道发生了什么.

解决方法

您在读取数据之前忘记了 flip的缓冲区,这就是为什么没有写入audioSamples的原因.
buffer.flip();
buffer.asShortBuffer.get(audioSamples);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读