@capacitor-community/native-audio

中文文档
8.0.0发布于 2025/12/30MITTypeScriptESM + CJS零依赖近 30 天下载 5.8万

A native plugin for native audio engine

原始使用文档
文档目录23 节
  1. Native Audio
  2. Maintainers
  3. Demo
  4. Preparation
  5. Installation
  6. Configuration
  7. Supported methods
  8. Usage
  9. API
  10. configure(...)
  11. preload(...)
  12. play(...)
  13. pause(...)
  14. resume(...)
  15. loop(...)
  16. stop(...)
  17. unload(...)
  18. setVolume(...)
  19. getCurrentTime(...)
  20. getDuration(...)
  21. isPlaying(...)
  22. addListener('complete', ...)
  23. Interfaces


Native Audio

@capacitor-community/native-audio

⚡ Capacitor plugin for playing sounds natively.


Maintainers

MaintainerGitHubSocial
Maxim Bazuevbazuka5801Telegram

Demo

YouTube Example

Preparation

All audio place in specific platform folder

Andoid: android/app/src/assets

iOS: ios/App/App/sounds

Web: assets/sounds

Installation

To use npm

npm install @capacitor-community/native-audio

To use yarn

yarn add @capacitor-community/native-audio

Sync native files

npx cap sync

On iOS, Android and Web, no further steps are needed.

Configuration

No configuration required for this plugin.

Supported methods

NameAndroidiOSWeb
configure✅✅❌
preload✅✅✅
play✅✅✅
pause✅✅✅
resume✅✅✅
loop✅✅✅
stop✅✅✅
unload✅✅✅
setVolume✅✅✅
getDuration✅✅✅
getCurrentTime✅✅✅
isPlaying✅✅✅

Usage

Example repository OR another complete Ionic/Angular application demonstrating every plugin method is available in the example-app directory

import {NativeAudio} from '@capacitor-community/native-audio'


/**
 * This method will load more optimized audio files for background into memory.
 * @param assetPath - relative path of the file or absolute url (file://)
 *        assetId - unique identifier of the file
 *        audioChannelNum - number of audio channels
 *        isUrl - pass true if assetPath is a `file://` url
 * @returns void
 */
NativeAudio.preload({
    assetId: "fire",
    assetPath: "fire.mp3",
    audioChannelNum: 1,
    isUrl: false
});

/**
 * This method will play the loaded audio file if present in the memory.
 * @param assetId - identifier of the asset
 * @param time - (optional) play with seek. example: 6.0 - start playing track from 6 sec
 * @returns void
 */
NativeAudio.play({
    assetId: 'fire',
    // time: 6.0 - seek time
});

/**
 * This method will loop the audio file for playback.
 * @param assetId - identifier of the asset
 * @returns void
 */
NativeAudio.loop({
  assetId: 'fire',
});


/**
 * This method will stop the audio file if it's currently playing.
 * @param assetId - identifier of the asset
 * @returns void
 */
NativeAudio.stop({
  assetId: 'fire',
});

/**
 * This method will unload the audio file from the memory.
 * @param assetId - identifier of the asset
 * @returns void
 */
NativeAudio.unload({
  assetId: 'fire',
});

/**
 * This method will set the new volume for a audio file.
 * @param assetId - identifier of the asset
 *        volume - numerical value of the volume between 0.1 - 1.0
 * @returns void
 */
NativeAudio.setVolume({
  assetId: 'fire',
  volume: 0.4,
});

/**
 * this method will get the duration of an audio file.
 * only works if channels == 1
 */
NativeAudio.getDuration({
  assetId: 'fire'
})
.then(result => {
  console.log(result.duration);
})

/**
 * this method will get the current time of a playing audio file.
 * only works if channels == 1
 */
NativeAudio.getCurrentTime({
  assetId: 'fire'
});
.then(result => {
  console.log(result.currentTime);
})

/**
 * This method will return false if audio is paused or not loaded.
 * @param assetId - identifier of the asset
 * @returns {isPlaying: boolean}
 */
NativeAudio.isPlaying({
  assetId: 'fire'
})
.then(result => {
  console.log(result.isPlaying);
})

API

configure(...)

configure(options: ConfigureOptions) => Promise<void>
ParamType
optionsConfigureOptions

preload(...)

preload(options: PreloadOptions) => Promise<void>
ParamType
optionsPreloadOptions

play(...)

play(options: { assetId: string; time?: number; }) => Promise<void>
ParamType
options{ assetId: string; time?: number; }

pause(...)

pause(options: { assetId: string; }) => Promise<void>
ParamType
options{ assetId: string; }

resume(...)

resume(options: { assetId: string; }) => Promise<void>
ParamType
options{ assetId: string; }

loop(...)

loop(options: { assetId: string; }) => Promise<void>
ParamType
options{ assetId: string; }

stop(...)

stop(options: { assetId: string; }) => Promise<void>
ParamType
options{ assetId: string; }

unload(...)

unload(options: { assetId: string; }) => Promise<void>
ParamType
options{ assetId: string; }

setVolume(...)

setVolume(options: { assetId: string; volume: number; }) => Promise<void>
ParamType
options{ assetId: string; volume: number; }

getCurrentTime(...)

getCurrentTime(options: { assetId: string; }) => Promise<{ currentTime: number; }>
ParamType
options{ assetId: string; }

Returns: Promise<{ currentTime: number; }>


getDuration(...)

getDuration(options: { assetId: string; }) => Promise<{ duration: number; }>
ParamType
options{ assetId: string; }

Returns: Promise<{ duration: number; }>


isPlaying(...)

isPlaying(options: { assetId: string; }) => Promise<{ isPlaying: boolean; }>
ParamType
options{ assetId: string; }

Returns: Promise<{ isPlaying: boolean; }>


addListener('complete', ...)

addListener(eventName: 'complete', listenerFunc: (event: { assetId: string; }) => void) => Promise<PluginListenerHandle>

Listen for asset completed playing event

ParamType
eventName'complete'
listenerFunc(event: { assetId: string; }) => void

Returns: Promise<PluginListenerHandle>

Since: 5.0.1


Interfaces

ConfigureOptions

PropTypeDescriptionDefault
fadebooleanIndicating whether or not to fade audio.false
focusbooleanIndicating whether or not to disable mixed audio.false

PreloadOptions

PropType
assetPathstring
assetIdstring
volumenumber
audioChannelNumnumber
isUrlboolean

PluginListenerHandle

PropType
remove() => Promise<void>

版本与发布记录

@capacitor-community/native-audio 共发布 16 个版本,首次发布于 2020/06/24;过去 12 个月发布了 1 个版本。

版本发布日期距今
8.0.0latest2025/12/3010 个月前
7.0.12025/09/211.1 年前
7.0.02025/03/281.6 年前
6.0.02024/06/292 年前
5.0.12023/07/223 年前
5.0.02023/07/203 年前

其他发布通道:next → 4.0.0-0。

关于 @capacitor-community/native-audio 的常见问题

@capacitor-community/native-audio 是什么?

A native plugin for native audio engine

如何安装 @capacitor-community/native-audio?

在项目目录运行 npm install @capacitor-community/native-audio,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@capacitor/core(>=8.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@capacitor-community/native-audio 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@capacitor-community/native-audio 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@capacitor-community/native-audio 可以商用吗?

@capacitor-community/native-audio 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@capacitor-community/native-audio 的下载量有多少?

根据 npm 官方下载统计,@capacitor-community/native-audio 在 2026/10/09 – 2026/11/07 这 30 天内被下载 58,128 次,日均约 1,938 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@capacitor-community/native-audio 还在维护吗?

最新版本 8.0.0 发布于 2025/12/30(10 个月前);至今共发布 16 个版本,其中过去 12 个月发布了 1 个;首次发布于 2020/06/24。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。