Last active
July 23, 2020 15:45
-
-
Save kayac-chang/db88df446cf5b6271a2fba41059bfcc5 to your computer and use it in GitHub Desktop.
Show how to integrate pixj.js loader with howler.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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