private AudioRecord audioRecord;
private Button start;
private Button stop;
private volatile int state;
private File outPutFile;
private File wavOutFile;
private AutomaticGainControl automaticGainControl = null;
private NoiseSuppressor noiseSuppressor = null;
private AcousticEchoCanceler acousticEchoCanceler = null;
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
quanxian();
setContentView(R.layout.activity_main);
initView();
int minBufferSize = AudioRecord.getMinBufferSize(44100, AudioFormat.CHANNEL_IN_STEREO, AudioFormat.ENCODING_PCM_16BIT);
audioRecord = new AudioRecord(
MediaRecorder.AudioSource.MIC,
44100,
AudioFormat.CHANNEL_IN_STEREO,
AudioFormat.ENCODING_PCM_16BIT,
minBufferSize
);
outPutFile = new File(Environment.getExternalStorageDirectory(),"audio.pcm");
if(!outPutFile.exists()){
try {
outPutFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
wavOutFile = new File(Environment.getExternalStorageDirectory(),"audio.wav");
if(!wavOutFile.exists()){
try {
wavOutFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
if(AutomaticGainControl.isAvailable()){
automaticGainControl = AutomaticGainControl.create(audioRecord.getAudioSessionId());
automaticGainControl.setEnabled(true);
}
if(NoiseSuppressor.isAvailable()){
noiseSuppressor = NoiseSuppressor.create(audioRecord.getAudioSessionId());
noiseSuppressor.setEnabled(true);
}
if(AcousticEchoCanceler.isAvailable()){
acousticEchoCanceler = AcousticEchoCanceler.create(audioRecord.getAudioSessionId());
acousticEchoCanceler.setEnabled(true);
}
}
private void initView() {
start = findViewById(R.id.btn_start);
stop = findViewById(R.id.btn_stop);
}
private Runnable runnable = new Runnable() {
OutputStream outputStream = null;
@Override
public void run() {
try {
outputStream = new FileOutputStream(outPutFile);
if(outputStream == null){
Log.e("录音","文件异常");
return;
}
byte[] bytes = new byte[1024];
while(state == 1){
int read = audioRecord.read(bytes, 0, bytes.length);
outputStream.write(bytes,0,read);
}
outputStream.close();
PcmToWav.copyWaveFile(outPutFile.getPath(),wavOutFile.getPath(),44100,1024);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
};
@RequiresApi(api = Build.VERSION_CODES.M)
private void quanxian() {
if(Build.VERSION.SDK_INT >= 16){
requestPermissions(new String[]{
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.RECORD_AUDIO
},100);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 100){
}
}
public void click(View view) {
switch (view.getId()){
case R.id.btn_start:
state = 1;
audioRecord.startRecording();
new Thread(runnable).start();
break;
case R.id.btn_stop:
audioRecord.stop();
state = 0;
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if(automaticGainControl!=null){
automaticGainControl = AutomaticGainControl.create(audioRecord.getAudioSessionId());
automaticGainControl.setEnabled(true);
}
if(noiseSuppressor.isAvailable()){
noiseSuppressor = NoiseSuppressor.create(audioRecord.getAudioSessionId());
noiseSuppressor.setEnabled(true);
}
if(acousticEchoCanceler.isAvailable()){
acousticEchoCanceler = AcousticEchoCanceler.create(audioRecord.getAudioSessionId());
acousticEchoCanceler.setEnabled(true);
}
}
public class PcmToWav {
public static void copyWaveFile(String inFileName, String outFileName, int sampleRateInHz, int bufferSizeInBytes) {
FileInputStream in = null;
FileOutputStream out = null;
long totalAudioLen = 0;
long totalDataLen = totalAudioLen + 36;
long longSampleRate = sampleRateInHz;
int channels = 2;
long byteRate = 16 * sampleRateInHz * channels / 8;
byte[] data = new byte[bufferSizeInBytes];
try {
in = new FileInputStream(inFileName);
out = new FileOutputStream(outFileName);
totalAudioLen = in.getChannel().size();
totalDataLen = totalAudioLen + 36;
writeWaveFileHeader(out, totalAudioLen, totalDataLen, longSampleRate, channels, byteRate);
while(in.read(data) != -1)
{
out.write(data);
}
in.close();
out.close();
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
public static void writeWaveFileHeader(FileOutputStream out, long totalAudioLen,
long totalDataLen, long longSampleRate, int channels, long byteRate)
throws IOException {
byte[] header = new byte[44];
header[0] = 'R';
header[1] = 'I';
header[2] = 'F';
header[3] = 'F';
header[4] = (byte) (totalDataLen & 0xff);
header[5] = (byte) ((totalDataLen >> 8) & 0xff);
header[6] = (byte) ((totalDataLen >> 16) & 0xff);
header[7] = (byte) ((totalDataLen >> 24) & 0xff);
header[8] = 'W';
header[9] = 'A';
header[10] = 'V';
header[11] = 'E';
header[12] = 'f';
header[13] = 'm';
header[14] = 't';
header[15] = ' ';
header[16] = 16;
header[17] = 0;
header[18] = 0;
header[19] = 0;
header[20] = 1;
header[21] = 0;
header[22] = (byte) channels;
header[23] = 0;
header[24] = (byte) (longSampleRate & 0xff);
header[25] = (byte) ((longSampleRate >> 8) & 0xff);
header[26] = (byte) ((longSampleRate >> 16) & 0xff);
header[27] = (byte) ((longSampleRate >> 24) & 0xff);
header[28] = (byte) (byteRate & 0xff);
header[29] = (byte) ((byteRate >> 8) & 0xff);
header[30] = (byte) ((byteRate >> 16) & 0xff);
header[31] = (byte) ((byteRate >> 24) & 0xff);
header[32] = (byte) (2 * 16 / 8);
header[33] = 0;
header[34] = 16;
header[35] = 0;
header[36] = 'd';
header[37] = 'a';
header[38] = 't';
header[39] = 'a';
header[40] = (byte) (totalAudioLen & 0xff);
header[41] = (byte) ((totalAudioLen >> 8) & 0xff);
header[42] = (byte) ((totalAudioLen >> 16) & 0xff);
header[43] = (byte) ((totalAudioLen >> 24) & 0xff);
out.write(header, 0, 44);
}
}