本文主要是介绍录音时分离左右声道的数据,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
平台录音默认为8通道数据,保存到文件中取左右声道数据。
/**
* 当mic=1时,取左声道数据
* 当mic=2时,取右声道数据
*
**/private byte[] splitStereoPcm(byte[] data) {int monoLength = data.length / 16;byte[] pcmData = new byte[monoLength*2];for (int i = 0; i < monoLength; i++) {if(mic == 1){System.arraycopy(data, i * 16, pcmData, i * 2, 2);}else if(mic == 2){System.arraycopy(data, i * 16 + 4, pcmData, i *2 , 2);}}return pcmData;}``````javaprivate AudioRecord mAudioRecord;private volatile boolean mIsRecording = false;private AudioFormat.Builder mAudioFormat;private AudioAttributes mPlaybackAttributes;private boolean mIsPlay;private AudioTrack mAudio = null;private RecorderHandler mHandler;/*** write sound file*/private void writeAudioDataToFile() {int bytesRecord = 0;int bufferSize = AudioRecord.getMinBufferSize(64000, AudioFormat.CHANNEL_IN_STEREO, AudioFormat.ENCODING_PCM_16BIT);//int bufferSize = AudioRecord.getMinBufferSize(64000, AudioFormat.CHANNEL_IN_STEREO, AudioFormat.ENCODING_PCM_16BIT);Log.d(TAG, "record bufferSize=(" + bufferSize + ")");byte[] tempBuffer = new byte[bufferSize];mAudioRecord.startRecording();requestFocus();mAudio.play();while (mIsPlay) {// gets the voice output from microphone to byte formatbytesRecord = mAudioRecord.read(tempBuffer, 0, bufferSize);Log.d(TAG, "record byte=(" + bytesRecord + ")");if (bytesRecord == AudioRecord.ERROR_INVALID_OPERATION || bytesRecord == AudioRecord.ERROR_BAD_VALUE) {Log.d(TAG, " Audio record read err");replyOperateResult(FAIL_RET);}if (bytesRecord != 0 && bytesRecord != -1) {mAudio.write(splitStereoPcm(tempBuffer), 0, bytesRecord / 8);} else {replyOperateResult(FAIL_RET);Log.d(TAG, "$$$record byte=0");}}mAudioRecord.stop();stopPlayBack();replyOperateResult(SUCCESS_RET);Log.d(TAG, "after writer record");}
这篇关于录音时分离左右声道的数据的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!