android AudioRecord幅度从MIC读取

时间:2012-05-14 07:33:45

标签: android audio record

我正在尝试录制来自MIC的声音并绘制实时图表。我能够记录和绘制图表。问题是使用下面的代码记录的值不准确,例如......下面的图像是我在没有声音的情况下得到的。我已经看过使用fft的例子但是我不确定在我的情况下是否会有任何帮助,因为我正在尝试绘制时域图,我认为没有目的将其转换为频域(现在)。其他人正在使用平均功率,这可能会有所帮助,但我不确定。

感谢您的帮助。

enter image description here

bufferSize = AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE,RECORDER_CHANNELS,RECORDER_AUDIO_ENCODING);

    recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
            RECORDER_SAMPLERATE, RECORDER_CHANNELS,RECORDER_AUDIO_ENCODING, bufferSize);

    short data [] = new short[bufferSize];

    while (!Thread.interrupted()) {

        recorder.startRecording();

        recorder.read(data, 0, bufferSize);

        recorder.stop();

        for (short s : data)
        {
            try {
                Thread.sleep((long) 300.00);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            postUI (Math.abs(s));
        }
    }

    recorder.release();

3 个答案:

答案 0 :(得分:15)

对于其他寻找方法的人,请查看Samsung's fantastic example complete with source code

Samsung example

答案 1 :(得分:1)

尝试使用更高的采样率。最大值为48000,但44100是标准配置。

此外,麦克风可能只是录制背景噪音。

答案 2 :(得分:0)

这是@Gourneau 的答案,但带有代码

AudioRecord recorder; // our recorder, must be initialized first
short[] buffer; // buffer where we will put captured samples
DataOutputStream output; // output stream to target file
boolean isRecording; // indicates if sound is currently being captured
ProgressBar pb; // our progress bar recieved from layout
while (isRecording) {
    double sum = 0;
    int readSize = recorder.read(buffer, 0, buffer.length);
    for (int i = 0; i < readSize; i++) {
        output.writeShort(buffer [i]);
        sum += buffer [i] * buffer [i];
    }
    if (readSize > 0) {
        final double amplitude = sum / readSize;
        pb.setProgress((int) Math.sqrt(amplitude));
    }
}

http://web.archive.org/web/20140709183733/http://developer.samsung.com/android/technical-docs/Displaying-Sound-Volume-in-Real-Time-While-Recording