pixi-audio is a plugin for Pixi.js v3.0.8 or higher to add Audio support using the pixi resource-loader. This plugin use WebAudio by default , and fallbacks to HTMLAudio if it's needed.
npm install pixi-audio
Browserify - webpack
if you use Browserify or Webpack you can use pixi-audio like this:
var PIXI = ;var audio = ; //pixi-audio is added automatically to the PIXI namespace
Just add pixi-audio.js under
pixi.js in your HTML file.
Loading and using Audio
To load audio files you need to use the resource-loader built in pixi.
Audio formats support
This lib support mp3, ogg, wav and m4a formats. Unfortunately all browsers doesn't support all this Audio formats, to solve this you can pass as an array some urls with different formats for the same file. So, the loader can load the correct format depending of the browser:
var files = "./assets/AwesomeMusic.ogg" "./assets/AwesomeMusic.mp3";PIXIloader;
In the above example, the audio loaded in chrome will be the ogg and in safari will be the mp3. But, you don't need care about this, just need to call getAudio using the audio name as param.
How it works
This plugin add a new namespace named
audio to the PIXI namespace. This namespace has some audio utils under the name
audioParser for the resource-loader, and two new classes:
AudioManager. Also, an instance of
AudioManager is added to
PIXI with the
audioManager name. So, you don't need do anything, just use PIXI.audioManager if you want.
It's important to know when you call
PIXI.audioManager.getAudio('myAudio') a new instance is created, so if you try again to get the same audio, it will be the same sound, but in a different instance.
Listening the basic events
var myAudio = PIXIaudioManager;myAudio;myAudio;myAudio;
Adding custom categories to our audios.
var myFX = PIXIaudioManager;myFXfx = true;var myFX2 = PIXIaudioManager;myFX2fx = true;//filtervar myFXAudios = PIXIaudioManager;forvar i = 0; i < myFXAudioslength; i++myFXAudiosi;
.getAudio( name )
Return a new instance of your sound (previously loaded).
.removeAudio( audio )
Remove the audio (instance) from the .sounds array.
.filterAudios( tag [, value] )
Return an array with all the audios matched with the tag and value passed.
Mute all the sounds in the sounds array.
Unmute all the sounds in the sounds array.
Pause all the sounds in the sounds array.
Resume all the sounds paused in the sounds array.
constructor( data, manager )
The constructor, usually you don't create an Audio, just call audioManager.getAudio(name).
The AudioManager instance who manage this audio.
The audio source
Read only, it's the state of this sound.
Set true to pause the sound and false to resume.
Set true if you want to play the sound in a loop.
The sound volume.
Set true if you want to mute the sound, or false if you want unmute.
Start playing the audio.
Stop playing the audio.
Remove this audio instance from the AudioManager