private void StartSendAudio() { // 8Khz 16bit 采样, iLBC 20ms encoder/decoder; iLbcEncSize=320byte int bytesRecord = 0, bytesEncoded = 0; long t_capture = 0; long t_process = 0; long delay = 0; short[] ecBuffer = new short[iLbcEncSize / 2]; short[] outBuffer = new short[iLbcEncSize / 2]; byte[] pcmOut = new byte[iLbcEncSize]; byte[] samples = new byte[iLbcEncSize]; byte[] data = new byte[iLbcDecsize]; while (isRecording == true) { // gp 有新生成文件标记时,生成新文件。 if (newFileFLag == true) { newFileFLag = false; new File(soundFNameString); try { braf = new RandomAccessFile(soundFNameString, "rw"); } catch (FileNotFoundException e) { e.printStackTrace(); } } if (braf == null) // null时,什么也不做但是循环继续 continue; t_capture = (new Date()).getTime(); bytesRecord = audioRecord.read(samples, 0, iLbcEncSize); ByteBuffer.wrap(samples).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(ecBuffer); t_process = (new Date()).getTime(); delay = (t_process - t_capture) + m_aecm.recvDelay; // 在类中多定义了一个变量 try { m_aecm.echoCancellation( ecBuffer, null, outBuffer, (short) (iLbcEncSize / 2), (short) (delay + 40)); } catch (Exception e1) { e1.printStackTrace(); } if (AudioRecord.ERROR_INVALID_OPERATION != bytesRecord) { ByteBuffer.wrap(pcmOut).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().put(outBuffer); bytesEncoded = AudioCodec.iLbcencode(pcmOut, 0, bytesRecord, data, 0); try { braf.write(data, 0, bytesEncoded); } catch (IOException e) { e.printStackTrace(); } } AudioCodec.iMVRSleepMS(10); } braf = null; soundFNameString = null; newFileFLag = false; }
public void run() { audioRecord.startRecording(); AudioCodec.iLbcinit(20); this.isRecording = true; StartSendAudio(); if (audioRecord != null) { audioRecord.stop(); audioRecord.release(); // 释放资源 audioRecord = null; } isRecording = false; }