音频录制和播放

录制:
int minBuffSize = AudioRecord.getMinBufferSize(8000,
AudioFormat.CHANNEL_CONFIGURATION_MONO,AudioFormat.ENCODING_PCM_16BIT);
AudioRecord mAudioRecorder = new AudioRecord(MediaRecorder.AudioSource.MIC, 8000,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT, minBuffSize*3);
//开始录音
mAudioRecorder.startRecording();

byte[] mBuffer = new byte[minBuffSize*3];
//从MIC获取音频数据到mBuffer中

int len = mAudioRecorder.read(mBuffer, 0, 1200);

播放:
int iMinBufSize = AudioTrack.getMinBufferSize(8000,
AudioFormat.CHANNEL_CONFIGURATION_STEREO,
AudioFormat.ENCODING_PCM_16BIT);
AudioTrack audioTrack=new AudioTrack(AudioManager.STREAM_MUSIC, 8000,
AudioFormat.CHANNEL_CONFIGURATION_STEREO, AudioFormat.ENCODING_PCM_16BIT,
iMinBufSize, AudioTrack.MODE_STREAM) ;

audioTrack.play();
audioTrack.write(data, 0, data.length);

你可能感兴趣的:(音频录制和播放)