Skip to content

@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 or recordingStopped.
  • 🤝 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:

npm install @capawesome-team/capacitor-audio-recorder
npx cap sync

Android

Permissions

This API requires the following permissions be added to your AndroidManifest.xml before or after the application tag:

<uses-permission android:name="android.permission.RECORD_AUDIO" />

Proguard

If you are using Proguard, you need to add the following rules to your proguard-rules.pro file:

-keep class io.capawesome.capacitorjs.plugins.** { *; }

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

cancelRecording()

cancelRecording() => Promise<void>

Cancel the recording.

Since: 7.0.0


getRecordingStatus()

getRecordingStatus() => Promise<GetRecordingStatusResult>

Check if the device supports audio recording.

Returns: Promise<GetRecordingStatusResult>

Since: 7.0.0


pauseRecording()

pauseRecording() => Promise<void>

Pause the recording.

This method is only available on Android (SDK 24+), iOS and Web.

Since: 7.0.0


resumeRecording()

resumeRecording() => Promise<void>

Resume the recording.

This method is only available on Android (SDK 24+), iOS and Web.

Since: 7.0.0


startRecording()

startRecording() => Promise<void>

Start recording audio in AAC format.

Since: 7.0.0


stopRecording()

stopRecording() => Promise<StopRecordingResult>

Stop recording audio.

Returns: Promise<StopRecordingResult>

Since: 7.0.0


checkPermissions()

checkPermissions() => Promise<PermissionStatus>

Check permissions for audio recording.

Returns: Promise<PermissionStatus>

Since: 7.0.0


requestPermissions()

requestPermissions() => Promise<PermissionStatus>

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.