pixijs/sound
حول
WebAudio API playback library, with filters. Modern audio playback for modern browsers.
التصميم والوسائطالنمو المقاس للنجوم
- خلال 24 ساعة
- ٠
- خلال 7 أيام
- +١
- خلال 15 يومًا
- +٢
- خلال شهر
- جارٍ جمع السجل
تغيّر صافي النجوم خلال فترات متداخلة، وليس مجموعًا يوميًا. تبقى القياسات المفقودة فارغة. النجوم تقيس الاهتمام لا جودة البرمجيات.
README.md
الأصلPixiJS Sound
WebAudio API playback library, with filters. Modern audio playback for modern browsers.
Features
- Pausing and resuming
- Independent volume control
- Support blocking or layered sounds (multiple instances)
- Support for
PIXI.Assetssystem - Dynamic filters:
- ReverbFilter
- DistortionFilter
- EqualizerFilter
- MonoFilter
- StereoFilter
- StreamFilter
- TelephoneFilter
Known Compatibility
- Any browser that PixiJS v8 runs in. WebAudio is used where available, with HTML audio as the fallback.
iOS silent mode
On iOS, the ringer/silent switch mutes WebAudio, but not HTML audio. This applies to every iOS browser. On iOS 17+, you can let sounds play in silent mode by setting the page's audio session before playing:
if ('audioSession' in navigator)
{
navigator.audioSession.type = 'playback';
}
A 'playback' session can pause audio from other apps, such as music, so only use it if that's acceptable for your app. On older iOS, setting sound.useLegacy = true before loading any sounds plays through HTML audio instead, without filters.
Usage
Installation is available by NPM:
npm i @pixi/sound --save
To import into your project, for instance, when using Webpack, Parcel, Rollup, or another bundler:
import { sound, Sound } from '@pixi/sound';
sound.add('my-sound', 'path/to/file.mp3');
sound.play('my-sound');
// Or create a sound without adding it to the library
const mySound = Sound.from('path/to/file.mp3');
mySound.play();
Classes and namespaces (Sound, filters, utils, etc.) are named exports. They are not properties of the sound object, so import them directly.
Assets
Importing @pixi/sound registers a loader with PIXI.Assets. Loaded sounds are added to the library under each of their aliases. Any sound option except url and preload, including sprites, can go in the asset's data. This works with Assets.add, Assets.addBundle and bundle manifests:
import { Assets } from 'pixi.js';
import { sound } from '@pixi/sound';
Assets.add({
alias: 'sfx',
src: 'path/to/sfx.{ogg,mp3}',
data: {
volume: 0.5,
sprites: {
coin: { start: 0, end: 0.4 },
jump: { start: 0.5, end: 1.1 },
},
},
});
await Assets.load('sfx');
sound.play('sfx', 'coin');
Channels
Channels need v6.1.0 or later.
A channel is a named group of sounds, such as music or effects, with its own volume, mute, pause and filters. A sound joins a channel by name in its options, and sound.channel(name) returns the channel, creating it the first time the name is used:
import { sound, filters } from '@pixi/sound';
sound.add('theme', { url: 'theme.mp3', channel: 'music' });
const music = sound.channel('music');
music.volume = 0.5;
music.muted = true;
music.paused = true; // a sound played into a paused channel starts paused
music.filters = [new filters.TelephoneFilter()]; // WebAudio only
music.stop(); // stops every sound in the channel
music.isPlaying; // true if any sound in the channel is playing, also while the channel is paused
The channel's volume is multiplied with the sound's and the global volume. volumeAll, muteAll and pauseAll don't change a channel's settings.
Streaming
Streaming needs v6.1.0 or later.
A long sound such as music can start playing while it downloads, instead of after the whole file has been downloaded and decoded. Set streaming on the sound:
import { sound } from '@pixi/sound';
import { Assets } from 'pixi.js';
sound.add('music', { url: 'music.mp3', streaming: true, loop: true });
sound.play('music');
// Or with Assets
Assets.add({ alias: 'music', src: 'music.mp3', data: { streaming: true } });
Volume, mute, speed, loop, pause, channels and the sound's and channel's filters still apply. A streaming sound plays one instance at a time (play() restarts it), and ignores end and play filters, so a sprite plays from its start to the end of the file. start needs a server that supports range requests, and a file on another origin needs CORS headers. If the browser blocks the play until the user interacts, it starts on the next click or tap. To start it from a key press, add the keyboard snippet in Autoplay and User Gestures.
Browser Usage
If you're using a <script> element to import @pixi/sound into your project, then the SoundLibrary object is PIXI.sound global. In the browser bundle, the named exports are also available on that global, which is why the examples use PIXI.sound.Sound and PIXI.sound.filters.
<!-- PixiJS must be imported before @pixi/sound -->
<script src="https://unpkg.com/pixi.js@8/dist/pixi.min.js"></script>
<!-- found here, if not using CDN "./node_modules/@pixi/sound/dist/pixi-sound.js" -->
<script src="https://unpkg.com/@pixi/sound@6/dist/pixi-sound.js"></script>
<script>
PIXI.sound.add('my-sound', 'path/to/file.mp3');
PIXI.sound.play('my-sound');
</script>
Autoplay and User Gestures
Browsers don't let a page start audio until the user has interacted with it. @pixi/sound creates its AudioContext on import, so it starts suspended and Chrome may log "The AudioContext was not allowed to start". That's expected. The library resumes the context on a mousedown, touchstart or touchend anywhere on the page, so a "click to start" screen is enough. If your app starts from the keyboard, resume it in your own handler:
import { sound } from '@pixi/sound';
document.addEventListener('keydown', () =>
{
if (!sound.context.paused)
{
sound.context.audioContext.resume();
}
});
Versions Compatibility
| PixiJS | PixiJS Sound | Status |
|---|---|---|
| v5.x - v6.x | v4.x | No longer maintained |
| v7.x | v5.x | No longer maintained |
| v8.x | v6.x | Maintained |
Resources
Running the Tests
The tests run in headless Chromium and Firefox through Vitest's browser mode, so they get a real AudioContext and HTMLAudioElement. They need Node.js 22.12 or later. Install Playwright's browsers once, then run them:
npm install
npx playwright install chromium firefox
npm test
To run one file, pass its path: npm test -- test/webaudio.test.ts. To run one browser, name it: npm test -- --project chromium. Firefox needs an audio output, so on a machine without one its tests time out.
CI also runs the tests in WebKit, where a failure doesn't block a merge. To run them, install it with npx playwright install webkit, then run npx vitest run --project webkit.
License
MIT License.
استكشف مشاريع مشابهة ومرتبطة
جارٍ التحميل…