原生音频__CAPGO_KEEP_0__插件:安装、设置和示例
返回插件
@capgo/native-audio
教程
由 github.com/Cap-go

原生音频

使用原生音频引擎在游戏和应用中播放短音频文件,低延迟

指南

本地音频教程

使用@capgo/native-audio

本地音频插件

安装

bun add @capgo/native-audio
bunx cap sync

此插件暴露的内容

  • configure - 配置音频播放器。
  • preload - 加载音频文件。
  • playOnce - 以自动清理方式播放音频文件一次。
  • isPreloaded - 检查是否已预加载音频文件。

示例用途

configure

配置音频播放器。

import { NativeAudio } from '@capgo/native-audio';

await NativeAudio.configure({} as ConfigureOptions);

preload

加载音频文件。

import { NativeAudio } from '@capgo/native-audio';

await NativeAudio.preload({} as PreloadOptions);

playOnce

自动清理后播放音频文件一次。

import { NativeAudio } from '@capgo/native-audio';

// Simple one-shot playback
await NativeAudio.playOnce({ assetPath: 'audio/notification.mp3' });

// Play and delete the file after completion
await NativeAudio.playOnce({
  assetPath: 'file:///path/to/temp/audio.mp3',
  isUrl: true,
  deleteAfterPlay: true
});

// Get the assetId to control playback
const { assetId } = await NativeAudio.playOnce({
  assetPath: 'audio/long-track.mp3',
  autoPlay: true
});
// Later, you can stop it manually if needed
await NativeAudio.stop({ assetId });

isPreloaded

检查是否已预加载音频文件。

import { NativeAudio } from '@capgo/native-audio';

await NativeAudio.isPreloaded({} as PreloadOptions);

全局参考