Android 使用AudioRecord录制音频时出现空白间隙

Android 使用AudioRecord录制音频时出现空白间隙,android,audio,record,audio-recording,Android,Audio,Record,Audio Recording,我使用AudioRecord从Android录制音频,然后使用AudioTrack回复。然而,结果非常糟糕,它在某种程度上与它录制的内容相似,但它可能会变慢(或者可能是因为它被修改了,所以我觉得是这样)。当我仔细分析它时,我意识到在我的short数组中有许多“间隙”(0值) 这是我收到的图表: 间隙(它重复这种模式,每630字节的数据,就有630字节的0): 这是我的录音代码: protected void onRecordButtonClick() { if (this.recor

我使用
AudioRecord
从Android录制音频,然后使用
AudioTrack
回复。然而,结果非常糟糕,它在某种程度上与它录制的内容相似,但它可能会变慢(或者可能是因为它被修改了,所以我觉得是这样)。当我仔细分析它时,我意识到在我的
short
数组中有许多“间隙”(0值)

这是我收到的图表:

间隙(它重复这种模式,每630字节的数据,就有630字节的0):

这是我的录音代码:

protected void onRecordButtonClick() {
    if (this.recording) {
        this.recording = false;
        this.butRecord.setText("Record");
    } else {
        this.recordBufferSize = AudioRecord.getMinBufferSize(44100, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);

        this.recorder = new AudioRecord(
                MediaRecorder.AudioSource.DEFAULT,
                44100,
                AudioFormat.CHANNEL_IN_MONO,
                AudioFormat.ENCODING_PCM_16BIT,
                this.recordBufferSize);

        this.recordThread = new Thread(new Runnable() {

            @Override
            public void run() {
                MainActivity.this.onRecording();
            }
        });
        this.recordThread.setPriority(Thread.MAX_PRIORITY);

        this.recording = true;
        this.butRecord.setText("Stop recording");
        this.recordThread.start();


    }

}

protected void onRecording() {
    this.recordData.clear();

    final short[] temp = new short[this.recordBufferSize];

    this.recorder.startRecording();

    while (this.recording) {
        this.recorder.read(temp, 0, this.recordBufferSize);

        for (int i = 0; i < temp.length; i ++) {
            this.recordData.add(temp[i]);
        }

        if (this.recordData.size() >= 220500) { this.recording = false; }
    }

    this.recorder.stop();

    // Complete data
    this.currentData = new short[this.recordData.size()];
    for (int i = 0; i < this.currentData.length; i++) {
        this.currentData[i] = this.recordData.get(i);
    }

    { // Write to SD Card the result
        final File file = new File(Environment.getExternalStorageDirectory(), "record.pcm");

        try {
            FileOutputStream fos = new FileOutputStream(file);
            ObjectOutputStream oos = new ObjectOutputStream(fos);

            for (int i = 0; i < this.currentData.length; i++) {
                oos.writeShort(this.currentData[i]);
            }

            oos.flush();
            oos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    this.handler.post(new Runnable() {

        @Override
        public void run() {
            MainActivity.this.waveform.setData(MainActivity.this.currentData);
            MainActivity.this.butRecord.setText("Record");

            final Bitmap bitmap = MainActivity.this.waveform.getDrawingCache();
            try {
                FileOutputStream fos = new FileOutputStream(new File(Environment.getExternalStorageDirectory(), "cache.png"));
                bitmap.compress(CompressFormat.PNG, 100, fos);
                fos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });


}
受保护的void onRecordButton单击(){
如果(本记录){
此项记录=错误;
this.butRecord.setText(“记录”);
}否则{
this.recordBufferSize=AudioRecord.getMinBufferSize(44100,单声道中的AudioFormat.CHANNEL\u,AudioFormat.ENCODING\u PCM\u 16位);
this.recorder=新的音频记录(
MediaRecorder.AudioSource.DEFAULT,
44100,
单声道中的AudioFormat.CHANNEL\u,
AudioFormat.ENCODING_PCM_16位,
这是一个新的参数(大小);
this.recordThread=新线程(new Runnable()){
@凌驾
公开募捐{
MainActivity.this.onRecording();
}
});
this.recordThread.setPriority(Thread.MAX_PRIORITY);
这是真的;
this.butRecord.setText(“停止录制”);
this.recordThread.start();
}
}
受保护的无效记录(){
此参数为.recordData.clear();
最终短路[]温度=新短路[此.recordBufferSize];
这个.recorder.startRecording();
而(这个录音){
this.recorder.read(temp,0,this.recordBufferSize);
对于(int i=0;i=220500){this.recording=false;}
}
这个.recorder.stop();
//完整数据
this.currentData=newshort[this.recordData.size()];
对于(int i=0;i
您找到解决方案了吗?我现在面临着一个类似的问题。不,我找不到任何解决办法。我改用LibGdx,因为它有简单的音频功能。只是为了比赛。