AudioRecorder
The AudioRecorder class allows you to record audio data to a file. It provides functionalities to start, stop, pause, and manage audio recordings, with configurable settings for audio quality, sample rate, format, and more.
Features
- Record audio from the system’s active input device.
- Record for a specified duration or until manually stopped.
- Pause and resume recordings.
- Delete recorded audio files.
Usage
Setup SharedAudioSesion
Before create an AudioRecorder instance, we need to setup the SharedAudioSession, the audio session is hardware-related and should be properly activated.
Creating an AudioRecorder Instance
To create an audio recorder, use the create method:
Recording Audio
You can start recording using the record() method:
You can also provide options to control when to start recording and for how long:
Pausing and Stopping the Recording
To pause a recording:
To stop a recording:
Deleting a Recording
To delete the recorded file:
Disposing of the Recorder
You should call dispose() when the recorder is no longer needed to free up resources:
Event Handling
You can use the onFinish and onError callbacks to handle recording completion and errors:
API Reference
AudioRecorder.create(filePath, settings?)
Creates an AudioRecorder instance with specified settings.
- filePath (string): The file system location to record to.
- settings (optional object): The audio settings for the recording:
- format (AudioFormat): The format of the audio data. Options: "LinearPCM", "MPEG4AAC", "AppleLossless", "AppleIMA4", "iLBC", "ULaw".
- sampleRate (number): The sample rate in hertz (8000 to 192000).
- numberOfChannels (number): The number of channels (1 to 64).
- encoderAudioQuality (AVAudioQuality): The quality of the audio encoding (from
AVAudioQuality.mintoAVAudioQuality.max).
Returns: A Promise that resolves with an AudioRecorder instance.
AudioRecorder.isRecording
A boolean indicating whether the recorder is recording.
AudioRecorder.currentTime
The time, in seconds, since the beginning of the recording.
AudioRecorder.deviceCurrentTime
The current time of the host audio device, in seconds.
AudioRecorder.record(options?)
Starts recording audio.
- options (optional object): Recording options:
- atTime (number): The time at which to start recording, relative to
deviceCurrentTime. - duration (number): The duration of the recording, in seconds.
- atTime (number): The time at which to start recording, relative to
Returns: A boolean indicating whether recording started successfully.
AudioRecorder.pause()
Pauses the current recording.
AudioRecorder.stop()
Stops recording and closes the audio file.
AudioRecorder.deleteRecording()
Deletes the recorded audio file.
Returns: A boolean indicating whether the deletion was successful.
AudioRecorder.dispose()
Releases resources used by the recorder.
AudioRecorder.onFinish
Callback function invoked when the recording finishes.
- success (boolean): Indicates whether the recording finished successfully.
AudioRecorder.onError
Callback function invoked when an encoding error occurs.
- message (string): Describes the error.
Example Usage
Use the AudioRecorder class to easily manage audio recording operations in your scripts, providing flexibility and control over the audio recording process.
