Android - 通过蓝牙麦克风实时录音可视化

问题描述

我使用 HFP 连接到蓝牙设备来获取音频。 我正在使用 AudioRecord 类以 44100 采样率使用 PCM 16 位编码进行录制。单声道 我还使用 simple library 来显示可视化

我的目标是显示正在录制的当前音频的某种可视化。 但是,我无法找到获取当前音频缓冲区的幅度/FFT 的方法。

我的录音/文件保存线程看起来像这样

 private class RecordingRunnable implements Runnable {

    @Override
    public void run() {
        final File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),"recording.pcm");
        final ByteBuffer buffer = ByteBuffer.allocateDirect(BUFFER_SIZE); //buffer size allocation
        try (final FileOutputStream outStream = new FileOutputStream(file)) {
            while (recordingInProgress.get()) {
                int result = recorder.read(buffer,BUFFER_SIZE); // reading
                if (result < 0) {
                    throw new RuntimeException("Reading of audio buffer failed: " +
                            getBufferReadFailureReason(result));
                }
               audioRecordView.update(); // What to write here so that i can get Amplitude/waveform of current audio.
                outStream.write(buffer.array(),BUFFER_SIZE); // writing buffer to file
                buffer.clear();
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("Writing of recorded audio failed",e);
        }

    }

正如您在上面的代码中看到的,方法 audioRecordView.update(); 是空的,因为我无法弄清楚如何获得音频信号的幅度。任何小提示/帮助/建议表示赞赏。

解决方法

这段代码对我有用

                    for (int i=0; i<result/2; i++) {
                    short curSample = getShort(buffer.array()[i*2],buffer.array()[i*2+1]);
                    if (curSample > cAmplitude) {
                        cAmplitude = curSample;
                    }
                }
             
                 audioRecordView.update(cAmplitude);

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...