
原声
@capacitor-community/native-audio
QQ 电容插件, 用于在本地播放声音 .
维护者
| 维护者 | GitHub 图像 | 社会 |
|---|---|---|
| 马克西姆·巴苏耶夫 | bazuka5801 (英语) | 电报 |
演示
筹备
特定平台文件夹中的所有音频位置
安道尔: android/app/src/assets
iOS: (英语) ios/App/App/sounds
网络 : assets/sounds
安装
用于 npm
npm install @capacitor-community/native-audio
用纱线
yarn add @capacitor-community/native-audio
同步本地文件
npx cap sync
在iOS,Android和Web上,不需要进一步的步骤.
配置
此插件不需要配置 .
支持的方法
| 名称 | 机器人 | iOS系统 | 网页 |
|---|---|---|---|
| 配置 | ✅ | ✅ | ❌ |
| 预装 | ✅ | ✅ | ✅ |
| 播放 | ✅ | ✅ | ✅ |
| 暂停 | ✅ | ✅ | ✅ |
| 恢复 | ✅ | ✅ | ✅ |
| 循环 | ✅ | ✅ | ✅ |
| 停下来 | ✅ | ✅ | ✅ |
| 卸货 | ✅ | ✅ | ✅ |
| 设置 | ✅ | ✅ | ✅ |
| 获取期限 | ✅ | ✅ | ✅ |
| 获取当前时间 | ✅ | ✅ | ✅ |
| 正在播放 | ✅ | ✅ | ✅ |
使用量
示例存储库 或 另一个完整的 Ionic/Angular 应用程序可以显示每个插件方法 示例应用程序 目录
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(options: ConfigureOptions) => Promise<void>
| 参数 | 类型 |
|---|---|
options | ConfigureOptions |
预装(...)
preload(options: PreloadOptions) => Promise<void>
| 参数 | 类型 |
|---|---|
options | PreloadOptions |
播放(...)
play(options: { assetId: string; time?: number; }) => Promise<void>
| 参数 | 类型 |
|---|---|
options | { assetId: string; time?: number; } |
暂停(...)
pause(options: { assetId: string; }) => Promise<void>
| 参数 | 类型 |
|---|---|
options | { assetId: string; } |
恢复(...)
resume(options: { assetId: string; }) => Promise<void>
| 参数 | 类型 |
|---|---|
options | { assetId: string; } |
循环(...)
loop(options: { assetId: string; }) => Promise<void>
| 参数 | 类型 |
|---|---|
options | { assetId: string; } |
停止(...)
stop(options: { assetId: string; }) => Promise<void>
| 参数 | 类型 |
|---|---|
options | { assetId: string; } |
卸载(...)
unload(options: { assetId: string; }) => Promise<void>
| 参数 | 类型 |
|---|---|
options | { assetId: string; } |
设置卷 (...)
setVolume(options: { assetId: string; volume: number; }) => Promise<void>
| 参数 | 类型 |
|---|---|
options | { assetId: string; volume: number; } |
获取当前时间(...)
getCurrentTime(options: { assetId: string; }) => Promise<{ currentTime: number; }>
| 参数 | 类型 |
|---|---|
options | { assetId: string; } |
回返情况: Promise<{ currentTime: number; }>
获取期限(...)
getDuration(options: { assetId: string; }) => Promise<{ duration: number; }>
| 参数 | 类型 |
|---|---|
options | { assetId: string; } |
回返情况: Promise<{ duration: number; }>
正在播放(...)
isPlaying(options: { assetId: string; }) => Promise<{ isPlaying: boolean; }>
| 参数 | 类型 |
|---|---|
options | { assetId: string; } |
回返情况: Promise<{ isPlaying: boolean; }>
添加倾听器( “ 完整 ” ,...)
addListener(eventName: 'complete', listenerFunc: (event: { assetId: string; }) => void) => Promise<PluginListenerHandle>
监听已完成的资产播放事件
| 参数 | 类型 |
|---|---|
eventName | 'complete' |
listenerFunc | (event: { assetId: string; }) => void |
回返情况: Promise<PluginListenerHandle>
自: 第5.0.1条
接口
配置选项
| Prop | 类型 | 说明 | 默认 |
|---|---|---|---|
fade | boolean | 表示是否要淡出音频. | false |
focus | boolean | 显示是否禁用混合音频 . | false |
预装选项
| Prop | 类型 |
|---|---|
assetPath | string |
assetId | string |
volume | number |
audioChannelNum | number |
isUrl | boolean |
插件听筒
| Prop | 类型 |
|---|---|
remove | () => Promise<void> |
