Zum Hauptinhalt springen
Zurück zu Plugins
@capgo/native-audio
Tutorial
von github.com/Cap-go

Native Audio

Wiedergabe kurzer Audio-Dateien mit geringer Latenz mithilfe des native Audio-Engines für Spiele und Apps

Anleitung

Tutorial zu Native Audio

Mit @capgo/native-audio

Ein natives Plugin für den native Audio-Engine.

Installieren

bun add @capgo/native-audio
bunx cap sync

Was dieses Plugin enthält

  • configure - Die Audio-Player-Konfiguration.
  • preload - Ein Audio-Datei laden.
  • playOnce - Ein Audio-Datei einmal mit automatischer Bereinigung abspielen.
  • isPreloaded - Überprüfen Sie, ob ein Audio-Datei vor dem Laden ist.

Beispiel für die Verwendung

configure

Konfigurieren Sie den Audio-Player.

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

await NativeAudio.configure({} as ConfigureOptions);

preload

Laden Sie eine Audio-Datei.

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

await NativeAudio.preload({} as PreloadOptions);

playOnce

Wiedergeben Sie eine Audio-Datei einmal mit automatischer Bereinigung.

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

Überprüfen Sie, ob eine Audio-Datei vor dem Laden ist.

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

await NativeAudio.isPreloaded({} as PreloadOptions);

Vollständige Referenz