Bonsai grows with the demand of its users and our flagship project pixelplant.com. The next big thing on our ever growing list of upcoming features was for Audio support. It’s taken us a little over a week to implement the first version and we’re pleased with the results. This version includes an API, a new AssetHandler, tests and a lot of example movies of course. It’s been great fun to replace the normal use of `console.log` with a sound-snippet. Suddenly we’re able to *hear* ticks, events, collisions and even bugs. Of course at some point we hit the boundaries of what is possible in the current web, especially on mobile devices but all in all we’re happy with what we’ve developed. The new Audio API enables a whole new media format and is a great way to express your creativity.

This is how the new API looks:

new Audio('myfile.mp3').addTo(stage).play();

So what happens here. We pass a URL to the Audio constructor and thereby create a new instance which also triggers the partial loading of the file. We add the instance to the stage and finally call `play()` what plays the audio as soon as enough data is available.

Press the play button and start the music.

Audio is our fifth supported asset type. We’re already able to handle Bitmaps, Fonts, Submovies and Videos. As with the other asset types, you’ll need to ensure that the file is loaded before it can be used. To do this you should listen for the “load” event and only then start to play.

new Audio('myfile.mp3').on('load', function() {
this.addTo(stage).play();
});

We know you’re a good web citizen and want to use more file-formats than just `audio/mp3` but also `audio/ogg` and probably more. The way to do that is to pass an Array of Objects (we call them AssetResourceObjects) to the Audio Constructor.

var assetRequest = [
{ src: 'myfile.mp3' },
{ src: 'myfile.ogg' },
{ src: 'myfile.eff', type: 'audio/exoticFileFormat' }
];
new Audio(assetRequest).on('load', function() {
this.addTo(stage).play();
});

To see the new Audio API in action we extended the existing Pong Game and equiped some collisions with sound. We hope you’ll experience the same difference as we did. Have fun!

We are looking forward to your feedback and seeing some amazing new Bonsai Movies from you.

Your Bonsai-Team