LynxAudioRecorder
Records audio from the microphone: a voice note, a voice message, or a meeting to transcribe. import { LynxAudioRecorder } from '@blotch/angular-lynx';
A recording follows how loud the person is as they speak. It stops into a clip: an .m4a file you can play back, share with LynxShare or upload.
Turn it on
Add permissions.microphone to the build target's options in angular.json, with the reason iOS shows when it asks:
What it adds:
- iOS:
NSMicrophoneUsageDescription, the reason iOS's prompt shows. - Android: the
RECORD_AUDIOpermission, which Android asks the person for the first time the app records. The microphone feature is declared not required, so Google Play still offers the app to devices without one.
Without it, the native code is left out of both apps. permission is then unsupported, and start() rejects with unavailable.
See Turn on device features for every setting.
Usage
The first start() asks the person for the microphone. Call requestPermission() first to ask at a moment of your choosing, such as after a screen that says what the microphone is for.
The note keeps its Send button once maxDuration stops it, and once the system pauses it. One recording is open at a time: a second start() rejects with busy until the first is stopped or canceled.
Pause, resume and the background
pause() and resume() carry on into the same clip, and the pause isn't counted in its length. The system pauses a recording too, and its state turns paused:
- iOS: a phone call, FaceTime or Siri taking the microphone, or the app going to the background.
- Android: the app going to the background.
A recording runs only while the app is in the foreground. Android gives an app in the background only silence, and iOS stops it without the background audio mode. Show a Resume button while it's paused.
Share or upload a clip
clip.uri is a file:// URL in the app's temporary folder, which the system may clear once the app isn't running. To keep it, save await clip.blob() with LynxFilePicker or upload it.
Delete a clip
clip.delete() deletes a stopped clip's file, such as once it's sent or the person discards it, rather than leaving it until the system clears the temporary folder. In the web preview it lets go of the clip's bytes and its blob: URL. After it, blob() rejects with failed. Calling it again does nothing, and neither does a file the system has already cleared.
cancel() ends a recording that's still open and deletes what it recorded, so it has no clip to delete.
Methods
permission is read again each time the app comes back to the foreground, so it turns granted after the person allows the microphone in Settings. Android can't tell an app whether it asked before, so there it's undetermined until the app asks.
AudioRecordingOptions
AudioRecording
level is the loudness from -60 dBFS (0) to full scale (1), the same on every platform, for a meter or a waveform.
AudioClip
A phone records AAC in mono at 44.1 kHz, asking the encoder for 64,000 bits a second.
AudioRecorderError
start(), stop() and clip.delete() reject with an AudioRecorderError, whose code is one of:
Platform support
Testing
The testing library fakes the microphone. The first start() asks and the person allows it; setMicrophonePermission() says otherwise, and setRecordedAudio() sets what a clip holds:
Related
- Permission reasons: the
microphonereason and the otherpermissions - Turn on device features: every opt-in build option
LynxShare: share a clip
