@capawesome-team/capacitor-audio-recorder¶
Capacitor plugin for audio recording using the device's microphone.
Features¶
- 🖥️ Cross-platform: Supports Android, iOS and Web.
- ⏯️ Full Control: Start, pause, resume, cancel and stop recording.
- 🔑 Permissions: Check and request microphone permissions.
- 🔊 Events: Listen for events like
recordingError
orrecordingStopped
. - 🤝 Compatibility: Compatible with the Speech Recognition, Speech Synthesis and Native Audio plugin.
- 📦 SPM: Supports Swift Package Manager for iOS.
- 🔁 Up-to-date: Always supports the latest Capacitor version.
- ⭐️ Support: First-class support from the Capawesome Team.
Compatibility¶
Plugin Version | Capacitor Version | Status |
---|---|---|
7.x.x | >=7.x.x | Active support |
Installation¶
This plugin is only available to Capawesome Insiders. First, make sure you have the Capawesome npm registry set up. You can do this by running the following commands:
npm config set @capawesome-team:registry https://npm.registry.capawesome.io
npm config set //npm.registry.capawesome.io/:_authToken <YOUR_LICENSE_KEY>
Attention: Replace <YOUR_LICENSE_KEY>
with the license key you received from Polar. If you don't have a license key yet, you can get one by becoming a Capawesome Insider.
Next, install the package:
Android¶
Permissions¶
This API requires the following permissions be added to your AndroidManifest.xml
before or after the application
tag:
Proguard¶
If you are using Proguard, you need to add the following rules to your proguard-rules.pro
file:
iOS¶
Privacy Descriptions¶
Add the NSMicrophoneUsageDescription
key to the ios/App/App/Info.plist
file, which tells the user why your app needs access to the user's contacts:
<key>NSMicrophoneUsageDescription</key>
<string>We need access to your microphone to record audio.</string>
Configuration¶
No configuration required for this plugin.
Usage¶
import { AudioRecorder } from '@capawesome-team/capacitor-audio-recorder';
import { NativeAudio } from '@capacitor-community/native-audio';
const cancelRecording = async () => {
await AudioRecorder.cancelRecording();
};
const getRecordingStatus = async () => {
const { status } = await AudioRecorder.getRecordingStatus();
console.log('Recording status:', status);
};
const pauseRecording = async () => {
await AudioRecorder.pauseRecording();
};
const resumeRecording = async () => {
await AudioRecorder.resumeRecording();
};
const startRecording = async () => {
await AudioRecorder.startRecording();
};
const stopRecording = async () => {
// Stop recording and get the audio blob or URI
const { blob, uri } = await AudioRecorder.stopRecording();
// Play the audio
if (blob) {
// Only available on Web
const audio = new Audio();
audio.src = URL.createObjectURL(blob);
audio.play();
} else if (uri) {
// Only available on Android and iOS
await NativeAudio.preload({
assetId: 'recording',
assetPath: uri,
isUrl: true,
});
await NativeAudio.play({ assetId: 'recording' });
}
};
const checkPermissions = async () => {
const { recordAudio } = await AudioRecorder.checkPermissions();
console.log('Record audio permission:', recordAudio);
};
const requestPermissions = async () => {
const { recordAudio } = await AudioRecorder.requestPermissions();
console.log('Record audio permission:', recordAudio);
};
const addRecordingErrorListener = async () => {
await AudioRecorder.addListener('recordingError', (event) => {
console.error('Recording error:', event.message);
});
};
const addRecordingStoppedListener = async () => {
await AudioRecorder.addListener('recordingStopped', (event) => {
console.log('Recording stopped:', event.uri);
});
};
API¶
- @capawesome-team/capacitor-audio-recorder
- Features
- Compatibility
- Installation
- Configuration
- Usage
- API
- cancelRecording()
- getRecordingStatus()
- pauseRecording()
- resumeRecording()
- startRecording()
- stopRecording()
- checkPermissions()
- requestPermissions()
- addListener('recordingError', ...)
- addListener('recordingStopped', ...)
- Interfaces
- GetRecordingStatusResult
- StopRecordingResult
- PermissionStatus
- PluginListenerHandle
- RecordingErrorEvent
- RecordingStoppedEvent
- Type Aliases
- PermissionState
- Enums
- RecordingStatus
- Changelog
- License
cancelRecording()¶
Cancel the recording.
Since: 7.0.0
getRecordingStatus()¶
Check if the device supports audio recording.
Returns: Promise<GetRecordingStatusResult>
Since: 7.0.0
pauseRecording()¶
Pause the recording.
This method is only available on Android (SDK 24+), iOS and Web.
Since: 7.0.0
resumeRecording()¶
Resume the recording.
This method is only available on Android (SDK 24+), iOS and Web.
Since: 7.0.0
startRecording()¶
Start recording audio in AAC format.
Since: 7.0.0
stopRecording()¶
Stop recording audio.
Returns: Promise<StopRecordingResult>
Since: 7.0.0
checkPermissions()¶
Check permissions for audio recording.
Returns: Promise<PermissionStatus>
Since: 7.0.0
requestPermissions()¶
Request permissions for audio recording.
Returns: Promise<PermissionStatus>
Since: 7.0.0
addListener('recordingError', ...)¶
addListener(eventName: 'recordingError', listenerFunc: (event: RecordingErrorEvent) => void) => Promise<PluginListenerHandle>
Called when an error occurs during recording. The recording will be cancelled.
Only available on iOS.
Param | Type |
---|---|
eventName |
'recordingError' |
listenerFunc |
(event: RecordingErrorEvent) => void |
Returns: Promise<PluginListenerHandle>
Since: 7.0.0
addListener('recordingStopped', ...)¶
addListener(eventName: 'recordingStopped', listenerFunc: (event: RecordingStoppedEvent) => void) => Promise<PluginListenerHandle>
Called when the recording is stopped.
Note: This will not be called if the recording is cancelled or paused or if an error occurs.
Param | Type |
---|---|
eventName |
'recordingStopped' |
listenerFunc |
(event: RecordingStoppedEvent) => void |
Returns: Promise<PluginListenerHandle>
Since: 6.0.0
Interfaces¶
GetRecordingStatusResult¶
Prop | Type | Description | Default | Since |
---|---|---|---|---|
status |
RecordingStatus |
The current recording status. | RecordingStatus.Inactive |
7.0.0 |
StopRecordingResult¶
Prop | Type | Description | Since |
---|---|---|---|
blob |
Blob |
The recorded audio as a Blob. Only available on Web. | 7.0.0 |
uri |
string |
The URI of the recorded audio. Only available on Android and iOS. | 7.0.0 |
PermissionStatus¶
Prop | Type | Description | Since |
---|---|---|---|
recordAudio |
PermissionState |
The permission state for audio recording. | 7.0.0 |
PluginListenerHandle¶
Prop | Type |
---|---|
remove |
() => Promise<void> |
RecordingErrorEvent¶
Prop | Type | Description | Since |
---|---|---|---|
message |
string |
The error message. | 7.0.0 |
RecordingStoppedEvent¶
Prop | Type | Description | Since |
---|---|---|---|
blob |
Blob |
The recorded audio as a Blob. Only available on Web. | 7.0.0 |
uri |
string |
The URI of the recorded audio. Only available on Android and iOS. | 7.0.0 |
Type Aliases¶
PermissionState¶
'prompt' | 'prompt-with-rationale' | 'granted' | 'denied'
Enums¶
RecordingStatus¶
Members | Value | Description | Since |
---|---|---|---|
Inactive |
'INACTIVE' |
The recording is inactive. | 7.0.0 |
Recording |
'RECORDING' |
The recording is active. | 7.0.0 |
Paused |
'PAUSED' |
The recording is paused. | 7.0.0 |
Changelog¶
See CHANGELOG.md.
License¶
See LICENSE.