enter code here
我正在尝试获得实时幅度。数据来自通过 MIC 连接到音频插孔的温度传感器。我需要使用 AudioRecord,因为我需要数据(原始数据)。我使用的代码如下:
public class MainActivity extends Activity {
private static final String LOG_TAG = "AudioRecordTest";
private static final float VISUALIZER_HEIGHT_DIP = 50f;
private static String mFileName = null;
private float temperature;
double sDataMax = 0;
String sDataString = "";
private boolean isRecording = false;
private Button mRecordButton = null;
private boolean mStartRecording = true;
private AudioRecord mRecorder = null;
private TextView Output;
private static final int samplingRate = 8000;
private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO;
private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
private void onRecord(boolean start) {
if (start) {
startRecording();
} else {
stopRecording();
}
}
int FileSize = AudioRecord.getMinBufferSize(samplingRate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
private void enableButton(int id, boolean isEnable) {
((Button) findViewById(id)).setEnabled(isEnable);
}
private void enableButtons(boolean isRecording) {
enableButton(R.id.btnRecord, !isRecording);
}
public void startRecording() {
mRecorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
samplingRate, RECORDER_CHANNELS,
RECORDER_AUDIO_ENCODING, FileSize*10);
mRecorder.startRecording();
isRecording = true;
}
private void writeAudioDataToFile(File logFile) {
// Write the output audio in byte
short sData[] = new short[FileSize];
while (isRecording) {
// gets the voice output from microphone to byte format
mRecorder.read(sData, 0, FileSize);
for (int i = 0; i < sData.length; i++) {
if (Math.abs(sData[i]) >= sDataMax) {
sDataMax = Math.abs(sData[i]);
}
;
sDataString += "\n" + sDataMax;
}