1

我使用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();
            }
        }
    });


}
4

0 回答 0