Howler.js PixiJS Audio Loader Parser
raw JSON →howler-pixi-loader-middleware is a utility package that enables PixiJS's asset loader to seamlessly integrate audio files using the Howler.js library. It maps loaded audio assets directly to `Howl` instances, simplifying audio management within PixiJS applications. The current stable version is 5.0.0, which specifically supports PixiJS v8.x and Howler.js v2.x. This package's release cadence is tightly coupled with major PixiJS releases, requiring updates to match new PixiJS loader APIs (e.g., transitioning from middleware to loader parser in v4 for PixiJS v7.x). Its primary differentiator is providing a direct, standardized way to load and manage audio through PixiJS's robust asset pipeline, making it a go-to choice for games and interactive experiences built with PixiJS that require advanced audio features from Howler.js, such as sprite management and Web Audio API capabilities. It abstracts away the need for manual Howler.js instantiation for assets loaded via Pixi's `Assets` system.
Common errors
error Uncaught TypeError: loader.use is not a function ↓
extensions.add(HowlerLoaderParser);. error Error: Asset for 'my_audio.mp3' not found in cache ↓
extensions.add(HowlerLoaderParser); is called before Assets.load('my_audio.mp3'); and that your howler-pixi-loader-middleware version is compatible with your pixi.js version (e.g., v5.x for PixiJS v8.x). error Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'play') ↓
HowlerLoaderParser is correctly added as an extension, and that howler.js is installed and functioning. Check browser console for earlier loading errors. Warnings
breaking Version 5.0.0 updates the package to support PixiJS v8.x exclusively, dropping compatibility with PixiJS v7.x. Applications using PixiJS v7.x must remain on howler-pixi-loader-middleware v4.x. ↓
breaking Version 4.0.0 introduced a significant API change for PixiJS v7.x. It transitioned from being a loader middleware (added to `loader.use()`) to a loader parser extension (added to `extensions.add()`). The old middleware approach will not work with v4.x and above. ↓
breaking Version 4.0.0 also dropped support for PixiJS v6.x. If you are using PixiJS v6.x or below, you must use an older version of this package (e.g., v3.x). ↓
gotcha Ensure `HowlerLoaderParser` is added to PixiJS extensions *before* any audio assets are loaded. If audio assets are loaded before the parser is registered, PixiJS will not know how to handle them, leading to loading failures. ↓
gotcha The package relies on `howler.js` and `pixi.js` as peer dependencies. Mismatched versions between these core libraries and this loader middleware can lead to runtime errors or unexpected behavior. ↓
Install
npm install howler-pixi-loader-middleware yarn add howler-pixi-loader-middleware pnpm add howler-pixi-loader-middleware Imports
- HowlerLoaderParser wrong
const HowlerLoaderParser = require('howler-pixi-loader-middleware');correctimport HowlerLoaderParser from 'howler-pixi-loader-middleware'; - Assets
import { Assets, extensions } from 'pixi.js'; - Howl
import { Howl } from 'howler';
Quickstart
import HowlerLoaderParser from 'howler-pixi-loader-middleware';
import { Assets, extensions } from 'pixi.js';
// Add the HowlerLoaderParser as a PixiJS extension before loading any audio assets.
// This tells PixiJS how to handle audio files with Howler.js.
extensions.add(HowlerLoaderParser);
async function loadAndPlayAudio() {
// Configure specific audio extensions to be handled by the parser, if not default
// For example, if you only want .mp3 and .ogg to be handled:
// HowlerLoaderParser.audioExtensions = ['.mp3', '.ogg'];
try {
// Load your audio file using PixiJS's Assets system.
// The loaded asset will be a Howl instance.
const myAudio = await Assets.load('my_audio.mp3');
console.log('Audio loaded successfully!', myAudio);
// Check if it's a Howl instance before playing
if (myAudio && typeof myAudio.play === 'function') {
myAudio.play();
console.log('Audio playing...');
// Example of stopping after a few seconds
setTimeout(() => {
myAudio.stop();
console.log('Audio stopped.');
}, 3000);
// Example of unloading the asset
// Assets.unload('my_audio.mp3');
} else {
console.error('Loaded asset is not a Howl instance.');
}
} catch (error) {
console.error('Failed to load audio:', error);
}
}
loadAndPlayAudio();