(new File(filePath)).delete(); } } if (audioRecorder != null) { audioRecorder.release(); } } /** * * * Resets the recorder to the INITIALIZING state, as if it was just created. * In case the class was in RECORDING state, the recording is stopped. * In case of exceptions the class is set to the ERROR state. * */ public void reset() { try { if (state != State.ERROR) { release(); filePath = null; // Reset file path audioRecorder = new AudioRecord(mAudioSource, sRate, nChannels, aFormat, mBufferSize); if (audioRecorder.getState() != AudioRecord.STATE_INITIALIZED) { throw new Exception("AudioRecord initialization failed"); } audioRecorder.setRecordPositionUpdateListener(updateListener); audioRecorder.setPositionNotificationPeriod(mPeriodInFrames); state = State.INITIALIZING; } } catch (Exception e) { Log.e(WavAudioRecorder.class.getName(), e.getMessage()); state = State.ERROR; } } /** * * * Starts the recording, and sets the state to RECORDING. * Call after prepare(). * */ public void start() { if (state == State.READY) { payloadSize = 0; audioRecorder.startRecording(); audioRecorder.read(buffer, 0, buffer.length); //[TODO: is this necessary]read the existing data in audio hardware, but don't do anything state = State.RECORDING; } else { Log.e(WavAudioRecorder.class.getName(), "start() called on illegal state"); state = State.ERROR; } } /** * * * Stops the recording, and sets the state to STOPPED. * In case of further usage, a reset is needed. * Also finalizes the wave file in case of uncompressed recording. * */ public void stop() { if (state == State.RECORDING) { audioRecorder.stop(); try { randomAccessWriter.seek(4); // Write size to RIFF header randomAccessWriter.writeInt(Integer.reverseBytes(36+payloadSize)); randomAccessWriter.seek(40); // Write size to Subchunk2Size field