Home

Awesome

ion.sound

JavaScript plugin for playing sounds on user actions and page events.


Description

Today websites are full of events (new mail, new chat-message, content update etc.). Often it is not enough to indicate this events only visually to get user attention. You need sounds! This library, made for playing small sounds, will help you with this task. Also, new version of Ion.Sound is capable to handle browser games audio. It has full control of loading, playing and removing audio files. And audio-sprites support of course.

Supported browsers

Desktop Windows, OS X, Linux:

Mobile:

Can i use <a href="http://caniuse.com/#feat=audio-api" target="_blank">Web Audio API</a> and <a href="http://caniuse.com/audio" target="_blank">HTML5 Audio</a>?

Demos

Dependencies

Usage

Import this library:

Prepare sound-files (25 sounds are included) and put them in some folder (eg. "sounds"):

It is not enough to have only MP3-file, you should make OGG and AAC-file too, because not all browsers support MP3.<br/> You can easily convert you MP3-s to OGG-s and AAC-s at <a href="http://media.io/" target="_blank">Media.io</a> or at <a href="https://cloudconvert.org/formats#audio" target="_blank">CloudConvert.org</a>.<br/> <i>AAC support was added to improve cross browser support of iOS 8.x devices (iPhone, iPad)</i>

Install with npm

Use NPM to download latest version of a plugin and install it directly in to your project.

Install with Yarn

Use Yarn to download latest version of a plugin and install it directly in to your project.

Initialisation

To initialise plugin call this method:

ion.sound({
    sounds: [
        {
            name: "my_cool_sound"
        },
        {
            name: "notify_sound",
            volume: 0.2
        },
        {
            name: "alert_sound",
            volume: 0.3,
            preload: false
        }
    ],
    volume: 0.5,
    path: "sounds/",
    preload: true
});

And play sound!

// Simple
ion.sound.play("my_cool_sound");

General settings

OptionDefaultsTypeDescription
sounds-arrayCollection of sound objects. Each object contains information about sound file and (optional) individual settings
path-stringPath to file
preloadfalsebooleanPreloading sounds
multiplayfalsebooleanSound multi play. If set, will allow to play multiple instances of one sound at once
loopfalseboolean/numberIf set to true will enable infinite loop. Or paste a number to set loop limit
volume1.0numberPlayback volume from 0 to 1
scopenullobjectCallbacks will be called in that object's scope
ready_callbacknullfunctionCalled after sound file is fully uploaded (or ready to play for HTML5 audio)
ended_callbacknullfunction``Called each time then sound file will reach it's end

Sound object

OptionDefaultsTypeDescription
name-stringFile name. Plugin will choose file extension automatically (.mp3, .ogg, .aac, .mp4 and etc.)
alias-stringAlias for sound call, optional. Normally is used to shorten ion.sound calls
sprite-objectMark that sound is audio-sprite. This is an object. Example: <code>{"part_name_1": [0, 2], "part_name_2": [2, 2]}</code><br/>Part_name is a name of sprite piece (it is used instead of name to play a sound). And array with time marks: [start, duration] in seconds.

And also individual: path, preload, multiplay, loop, volume, scope and callbacks

Plugin can be launched in jQuery namespace

<a href="history.md">Update history</a>


Support Ion-series plugins development: