Skip to content

Instantly share code, notes, and snippets.

@kayac-chang
Last active July 23, 2020 15:45
Show Gist options
  • Save kayac-chang/db88df446cf5b6271a2fba41059bfcc5 to your computer and use it in GitHub Desktop.
Save kayac-chang/db88df446cf5b6271a2fba41059bfcc5 to your computer and use it in GitHub Desktop.
Show how to integrate pixj.js loader with howler.js
import { Loader } from 'pixi.js';
import { Howl } from 'howler';
const loader = new Loader();
loader.pre(SoundHandler);
function SoundHandler(resource, next) {
const SUPPORT_FORMATS = ['mp3', 'opus', 'ogg', 'wav', 'aac', 'm4a', 'm4b', 'mp4', 'webm'];
if (!SUPPORT_FORMATS.includes(resource.extension)) {
return next();
}
const sound = new Howl({
src: resource.url,
onload,
onloaderror,
});
function onload() {
resource.complete();
resource.data = sound;
next();
}
function onloaderror(soundId, error) {
resource.abort(error);
console.error(resource, error);
next();
}
}
async function load(pkg) {
for (const [name, url] of Object.entries(pkg)) {
if (loader.resources[name]) {
continue;
}
loader.add(name, url);
}
return new Promise((resolve, reject) => {
loader.load(resolve);
loader.onError.add(reject);
});
}
function getTexture(res) {
return loader.resources[res].texture;
}
function getSound(res) {
return loader.resources[res].data;
}
export default {
load,
getTexture,
getSound,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment