如何使用AUDIORECORD在Android中录制音频时检测静音

时间:2016-08-29 13:49:31

标签: java android

试图检测沉默。使用以下代码。我的目标是将所有录制的数据转换为我实现的.wav文件,但现在我想在检测到静音时停止录制。这可以实现吗?

mAud.startRecording();
            while (mIsRecording) {
                ByteBuffer buffer = ByteBuffer.allocateDirect(mBufferSize * 2);
                numSamples = mAud.read(buffer, mBufferSize * 2);
                if (numSamples == AudioRecord.ERROR_INVALID_OPERATION) {
                    throw new IllegalStateException(
                            "read() returned AudioRecord.ERROR_INVALID_OPERATION");
                } else if (numSamples == AudioRecord.ERROR_BAD_VALUE) {
                    throw new IllegalStateException(
                            "read() returned AudioRecord.ERROR_BAD_VALUE");
                }
                if (numSamples > 0)
                    addBuffer(buffer, numSamples / 2);
            }
            mAud.stop();
            mAud.release();

0 个答案:

没有答案