快速入门
复制包含安装步骤和该插件完整 Markdown 指南的配置 prompt。
Set up this Capacitor plugin in the project.
Use the package manager already used by the project.
Install these package(s): `@capgo/capacitor-audio-recorder`
Run the required Capacitor sync/update step after installation.
Read this markdown guide for the full setup steps: https://raw.githubusercontent.com/Cap-go/website/refs/heads/main/apps/docs/src/content/docs/docs/plugins/audio-recorder/getting-started.mdx
Use that guide for platform-specific steps, native file edits, permissions, config changes, imports, and usage setup.
If that guide references other docs pages, read them too.
安装
“安装”章节你可以使用我们的 AI 辅助设置来安装插件。使用以下命令将 Capgo skills 添加到你的 AI 工具中:
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins然后使用以下 prompt:
Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/capacitor-audio-recorder` plugin in my project.如果你更想手动设置,请运行以下命令安装插件,并按照下方各平台的说明操作:
bun add @capgo/capacitor-audio-recorderbunx cap sync导入
“导入”章节import { CapacitorAudioRecorder } from '@capgo/capacitor-audio-recorder';快速入门
章节标题为“快速入门”请求麦克风权限,录音,然后停止并读取原生文件 URI(或 Web Blob).
import { Capacitor } from '@capacitor/core';import { CapacitorAudioRecorder } from '@capgo/capacitor-audio-recorder';
const mic = await CapacitorAudioRecorder.requestPermissions();if (mic.recordAudio !== 'granted') { throw new Error('Microphone permission required');}
await CapacitorAudioRecorder.startRecording({ sampleRate: 44100, bitRate: 128000, resetToPlaybackOnStop: true, // iOS only: restore playback-friendly audio session after stop});
const { uri, blob, duration } = await CapacitorAudioRecorder.stopRecording();const clipRef = Capacitor.getPlatform() === 'web' ? blob : uri;console.log('Saved clip', clipRef, duration);示例:带暂停功能的语音备忘录
章节标题为“示例:带暂停功能的语音备忘录”await CapacitorAudioRecorder.startRecording();// user taps pause during a long noteawait CapacitorAudioRecorder.pauseRecording();await CapacitorAudioRecorder.resumeRecording();const clip = await CapacitorAudioRecorder.stopRecording();pauseRecording 适用于 Android (API 24+)、iOS 和 Web。
示例:实时电平表
章节标题为“示例:实时电平表”轮询 getCurrentAmplitude() 通过定时器(约 60 到 100 ms)进行,当状态为 RECORDING。数值已归一化 0 改为 1, 但底层信号因平台而异(Android 为峰值,iOS 为平均功率,Web 为 RMS)。如果需要视觉一致,请按平台做小幅缩放。
const tick = async () => { const { status } = await CapacitorAudioRecorder.getRecordingStatus(); if (status !== 'RECORDING') return; const { value } = await CapacitorAudioRecorder.getCurrentAmplitude(); updateMeterUi(value);};示例:丢弃一条录音
章节标题:“示例:丢弃一次录制”await CapacitorAudioRecorder.startRecording();await CapacitorAudioRecorder.cancelRecording(); // no file persisted常见坑点
章节标题为“常见陷阱”- Web 与原生返回结构:Android 和 iOS 返回
uri来自stopRecording。Web 返回blob(并且可以省略uri). - iOS 上录音后的静音播放:设置
resetToPlaybackOnStop: true中的startRecording如果用户在同一会话中回放该片段。 - 后台录音: 该插件本身不会启用后台音频。在 iOS 上,如果需要在锁屏时进行录音,请添加 Audio background mode。
- 避免频繁调用 amplitude: 每次
getCurrentAmplitude: 调用都会经过 bridge。请使用 UI interval,不要用 tight loop。
API 概览
章节标题:“API 概览”startRecording
标题为 “startRecording” 的章节使用设备麦克风开始录音。
import { CapacitorAudioRecorder } from '@capgo/capacitor-audio-recorder';
await CapacitorAudioRecorder.startRecording();pauseRecording
“pauseRecording” 章节暂停正在进行的录音。仅支持 Android (API 24+)、iOS 和 Web。
import { CapacitorAudioRecorder } from '@capgo/capacitor-audio-recorder';
await CapacitorAudioRecorder.pauseRecording();resumeRecording
“resumeRecording” 章节恢复之前暂停的录音。
import { CapacitorAudioRecorder } from '@capgo/capacitor-audio-recorder';
await CapacitorAudioRecorder.resumeRecording();stopRecording
“stopRecording” 章节停止当前录音并保存录制的音频。
import { CapacitorAudioRecorder } from '@capgo/capacitor-audio-recorder';
const result = await CapacitorAudioRecorder.stopRecording();console.log(result);cancelRecording
“cancelRecording” 章节取消当前录音并丢弃已捕获的音频。
import { CapacitorAudioRecorder } from '@capgo/capacitor-audio-recorder';
await CapacitorAudioRecorder.cancelRecording();getRecordingStatus
“getRecordingStatus” 章节获取当前录音状态。
import { CapacitorAudioRecorder } from '@capgo/capacitor-audio-recorder';
const result = await CapacitorAudioRecorder.getRecordingStatus();console.log(result);getCurrentAmplitude
“getCurrentAmplitude” 章节获取当前输入振幅(麦克风电平),返回归一化
数值,范围在 [0, 1] 指定区间内。
用于在录音时驱动 VU 表或波形图等实时可视化。返回 0 无录音时的值。专为按 UI 刷新率轮询而设计——对波形图来说,60–100 ms 的间隔是个不错的起点。避免在紧凑循环中调用;每次调用都会跨越 JS/native bridge。
import { CapacitorAudioRecorder } from '@capgo/capacitor-audio-recorder';
const result = await CapacitorAudioRecorder.getCurrentAmplitude();console.log(result);checkPermissions
“checkPermissions” 章节返回麦克风访问权限的当前状态。
import { CapacitorAudioRecorder } from '@capgo/capacitor-audio-recorder';
const result = await CapacitorAudioRecorder.checkPermissions();console.log(result);requestPermissions
“requestPermissions” 章节请求访问麦克风的权限。
import { CapacitorAudioRecorder } from '@capgo/capacitor-audio-recorder';
const result = await CapacitorAudioRecorder.requestPermissions();console.log(result);类型参考
“类型参考” 章节StartRecordingOptions
“StartRecordingOptions” 章节支持的选项。
export interface StartRecordingOptions { /** * The audio session category options for recording. Only available on iOS. * * @since 1.0.0 */ audioSessionCategoryOptions?: AudioSessionCategoryOption[];
/** * The audio session mode for recording. Only available on iOS. * * @since 1.0.0 */ audioSessionMode?: AudioSessionMode;
/** * The audio bit rate in bytes per second. * Only available on Android and iOS. * * @since 1.0.0 */ bitRate?: number;
/** * The audio sample rate in Hz. * Only available on Android and iOS. * * @since 1.0.0 */ sampleRate?: number;}StopRecordingResult
“StopRecordingResult” 章节由 . 返回的结果。
export interface StopRecordingResult { /** * The recorded audio as a Blob. Only available on Web. * * @since 1.0.0 */ blob?: Blob;
/** * The duration of the recording in milliseconds. * * @since 1.0.0 */ duration?: number;
/** * The URI pointing to the recorded file. Only available on Android and iOS. * * @since 1.0.0 */ uri?: string;}GetRecordingStatusResult
“GetRecordingStatusResult” 章节由 . 返回的结果。
export interface GetRecordingStatusResult { /** * The current recording status. * * @since 1.0.0 */ status: RecordingStatus;}GetCurrentAmplitudeResult
“GetCurrentAmplitudeResult” 章节由 . 返回的结果。
export interface GetCurrentAmplitudeResult { /** * The current input amplitude normalized to the `[0, 1]` range, where `0` * represents silence and `1` represents the maximum level the platform can * report. The value is `0` when no recording is active. * * Note: the source signal differs between platforms — Android reports the * peak sample amplitude since the last call, iOS reports the average power * in dB converted to linear, and Web reports the RMS of the latest frame. * Consumers that need cross-platform parity may want to apply a * per-platform scaling curve. * * @since 8.1.0 */ value: number;}PermissionStatus
标题为“PermissionStatus”的章节由 和 返回的权限信息。
export interface PermissionStatus { /** * The permission state for audio recording. * * @since 1.0.0 */ recordAudio: PermissionState;}RecordingErrorEvent
“RecordingErrorEvent” 章节录音过程中发生错误时触发的事件。
export interface RecordingErrorEvent { /** * The error message. * * @since 1.0.0 */ message: string;}RecordingStoppedEvent
“RecordingStoppedEvent” 章节录音完成后触发的事件。
export type RecordingStoppedEvent = StopRecordingResult;AudioSessionCategoryOption
标题为 “AudioSessionCategoryOption” 的章节iOS 上可用的 Audio session category 选项。
export enum AudioSessionCategoryOption { AllowAirPlay = 'ALLOW_AIR_PLAY', AllowBluetooth = 'ALLOW_BLUETOOTH', AllowBluetoothA2DP = 'ALLOW_BLUETOOTH_A2DP', DefaultToSpeaker = 'DEFAULT_TO_SPEAKER', DuckOthers = 'DUCK_OTHERS', InterruptSpokenAudioAndMixWithOthers = 'INTERRUPT_SPOKEN_AUDIO_AND_MIX_WITH_OTHERS', MixWithOthers = 'MIX_WITH_OTHERS', OverrideMutedMicrophoneInterruption = 'OVERRIDE_MUTED_MICROPHONE_INTERRUPTION',}AudioSessionMode
标题为 “AudioSessionMode” 的章节iOS 上可用的 Audio session 模式。
export enum AudioSessionMode { Default = 'DEFAULT', GameChat = 'GAME_CHAT', Measurement = 'MEASUREMENT', SpokenAudio = 'SPOKEN_AUDIO', VideoChat = 'VIDEO_CHAT', VideoRecording = 'VIDEO_RECORDING', VoiceChat = 'VOICE_CHAT',}RecordingStatus
标题为 “RecordingStatus” 的章节录音状态。
export enum RecordingStatus { Inactive = 'INACTIVE', Recording = 'RECORDING', Paused = 'PAUSED',}PermissionState
“PermissionState” 章节Capacitor 支持的平台权限状态。
export type PermissionState = 'prompt' | 'prompt-with-rationale' | 'granted' | 'denied';单一事实来源
标题为“单一事实来源”的部分本页面由插件的 src/definitions.ts生成。当上游的公共 API 发生变化时,请重新运行 sync。
从入门指南继续
“从入门指南继续”章节如果你正在使用 快速入门 来规划原生媒体和界面行为,并将其与 使用 @capgo/capacitor-audio-recorder 在“使用 @capgo/capacitor-audio-recorder”中的原生功能 使用 @capgo/capacitor-live-activities 用于使用 @capgo/capacitor-live-activities 时的原生能力, @capgo/capacitor-live-activities 在 @capgo/capacitor-live-activities 中的实现细节, 使用 @capgo/capacitor-video-player 有关使用 @capgo/capacitor-video-player 时的原生能力,以及 @capgo/capacitor-video-player 关于 @capgo/capacitor-video-player. 中的实现细节