Giter Club home page Giter Club logo

pixi-audio's Introduction

pixi-audio

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.

Installation

npm install pixi-audio

Usage

Browserify - webpack

if you use Browserify or Webpack you can use pixi-audio like this:

var PIXI = require('pixi');
var audio = require('pixi-audio'); //pixi-audio is added automatically to the PIXI namespace

Prebuilt files

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.

PIXI.loader.add([
  {name:"AwesomeMusic", url:"./assets/AwesomeMusic.ogg"}
]).load(function(){
  var awesomeMusic = PIXI.audioManager.getAudio('AwesomeMusic');
  awesomeMusic.play();
});

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"];

PIXI.loader.add([
  {name:"AwesomeMusic", url: files}
]).load(function(){
  var awesomeMusic = PIXI.audioManager.getAudio('AwesomeMusic');
  awesomeMusic.play();
});

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 utils, the audioParser for the resource-loader, and two new classes: Audio and 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.

Events

The Audio class extends from PIXI.utils.EventEmitter, and emit some events: play, stop, end, pause and resume. This events name are so clear about what they doing. More info: Node.js Events

Some examples

Listening the basic events

var myAudio = PIXI.audioManager.getAudio('myAudio');
myAudio.on('start', function(){ console.log('Start!!'); });
myAudio.on('end', function(){ console.log('End!!'); });

myAudio.play();

Adding custom categories to our audios.

var myFX = PIXI.audioManager.getAudio('myAudio1');
myFX.fx = true;
var myFX2 = PIXI.audioManager.getAudio('myAudio2');
myFX2.fx = true;

//filter
var myFXAudios = PIXI.audioManager.filterAudios('fx');
for(var i = 0; i < myFXAudios.length; i++)myFXAudios[i].stop();

api

AudioManager

constructor()

The constructor #### .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()

Mute all the sounds in the sounds array.

.unmute()

Unmute all the sounds in the sounds array.

.pause()

Pause all the sounds in the sounds array.

.resume()

Resume all the sounds paused in the sounds array.

Audio

constructor( data, manager )

The constructor, usually you don't create an Audio, just call audioManager.getAudio(name).

.manager

The AudioManager instance who manage this audio.

.data

The audio source

.playing

Read only, it's the state of this sound.

.paused

Set true to pause the sound and false to resume.

.loop

Set true if you want to play the sound in a loop.

.volume

The sound volume.

.muted

Set true if you want to mute the sound, or false if you want unmute.

.play()

Start playing the audio.

.stop()

Stop playing the audio.

.remove()

Remove this audio instance from the AudioManager

pixi-audio's People

Contributors

nazariglez avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

pixi-audio's Issues

Web audio as an option?

Hi,
Thanks for the audio solution to PIXI project.

We found some weird Web Audio issues on mobile browsers so would like to try HTML Audio instead of Web Audio. But can't find the way to do it.

Difference between end and stop event.

Please consider the following code:

PIXI.loader.add([{name: 'anchordown', url: anchorDownSound }]).load(function(){
  anchorDownSound = PIXI.audioManager.getAudio('anchordown');
  anchorDownSound.on('end', function(){
    anchorDown = true;
  });
});

when I call anchorDownSound.play() the sound plays and when the sound track finishes playing at the end, I correctly get the 'end' event and I can set my anchorDown flag.

However, when I call anchorDownSound.stop() before the file finishes playing I get the 'end' event as well. The expected behavior was to only receive a 'stop' event but not an 'end' event.

I only want the anchorDown flag to be set when the file completed playback to the end.

issue with completed audios resume on resume() call

I unminified the code and fixed the issue for my needs, but when you have played sounds previously they start again when resume is called even though they are not playing.

for my fix i just added a check to see if it was playing before pausing / resuming
if (this.sounds[i].playing) this.sounds[i].paused = e

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.