JavaScript plugin for playing sounds and music in browsers

Overview

ion.sound

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


Description

  • Ion.Sound — JavaScript-plugin for playing sounds based on Web Audio API.
  • Plugin is working on most popular desktop and mobile browsers and can be used everywhere, from common web sites to browser games.
  • For not so modern browsers plugin falls back to HTML5 audio.
  • Audio-sprites support included.
  • Ion.Sound freely distributed under terms of MIT licence.
  • 25 free sounds included

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:

  • Google Chrome
  • Mozilla Firefox
  • Microsoft Internet Explorer 9.0+
  • Opera 12.16+
  • Safari 5.1+ (Safari on Windows requires QuickTime to play sounds)

Mobile:

  • iOS Safari and others (with some restrictions)
  • Android Google Chrome and others (with some restrictions also)
  • WP8 Internet Explorer

Can i use Web Audio API and HTML5 Audio?

Demos

Dependencies

  • None

Usage

Import this library:

  • ion.sound.min.js

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

  • my_cool_sound.mp3
  • my_cool_sound.ogg
  • my_cool_sound.aac

It is not enough to have only MP3-file, you should make OGG and AAC-file too, because not all browsers support MP3.
You can easily convert you MP3-s to OGG-s and AAC-s at Media.io or at CloudConvert.org.
AAC support was added to improve cross browser support of iOS 8.x devices (iPhone, iPad)

Install with npm

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

  • npm install ion-sound

Install with Yarn

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

  • yarn add ion-sound

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

Option Defaults Type Description
sounds - array Collection of sound objects. Each object contains information about sound file and (optional) individual settings
path - string Path to file
preload false boolean Preloading sounds
multiplay false boolean Sound multi play. If set, will allow to play multiple instances of one sound at once
loop false boolean/number If set to true will enable infinite loop. Or paste a number to set loop limit
volume 1.0 number Playback volume from 0 to 1
scope null object Callbacks will be called in that object's scope
ready_callback null function Called after sound file is fully uploaded (or ready to play for HTML5 audio)
ended_callback null function`` Called each time then sound file will reach it's end

Sound object

Option Defaults Type Description
name - string File name. Plugin will choose file extension automatically (.mp3, .ogg, .aac, .mp4 and etc.)
alias - string Alias for sound call, optional. Normally is used to shorten ion.sound calls
sprite - object Mark that sound is audio-sprite. This is an object. Example: {"part_name_1": [0, 2], "part_name_2": [2, 2]}
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

  • Use aliases to call any plugin methods: ion.sound(); -> $.ionSound();
  • ion.sound.play("sound_name"); -> $.ionSound.play("sound_name");
  • Etc.

Update history


Support Ion-series plugins development:

Comments
  • Sound playing in mobile Safari but not in fullscreen mode from the home screen (iPad)

    Sound playing in mobile Safari but not in fullscreen mode from the home screen (iPad)

    I'm using jQuery.ionSound version 1.3.0 on iOS 8.1.1, but the issue already existed in iOS 7.

    When I visit my site using mobile Safari, everything is fine and sounds play normally. However, when I add my site to the home screen and visit it from there, sounds don't play.

    This might be a hard restriction imposed by iOS (which I'd like to know for sure), but if it isn't, I hope you can fix it.

    Bug 
    opened by jgonggrijp 16
  • Sound doesnt play after a call

    Sound doesnt play after a call

    Hello, I have an app mobile and it is working fine with ion js, but in some cases when the mobiles got a call it happens that, after the call is finished, the sound doesnt play. Is there a way to re-load the driver or other way to fix the problem? thanks.

    Question 
    opened by pmoitech 12
  • Sound plays on mobile app muted

    Sound plays on mobile app muted

    Hello guys as the title says im having issued, i added this library for a mobile web app that im working on, it works well but turns out that the sound is beign played even when the mobile is muted, or in vibartinon mode, im calling the sound via onclick event. How can i fix this? is there any way?

    Question 
    opened by CoericK 10
  • volume:0  not working

    volume:0 not working

    Hi, I was hoping that sound should play based on user pref stored , so when i play sound like

    ion.sound.play("pop_cork", {
          volume: 0
    });
    

    Sound is still audible with intensity set at base ? is this required behavior ?

    Bug 
    opened by mafar 9
  • Support multiple sound

    Support multiple sound

    After trying many sound library in js, finally your lib can help me play some sound(aac - iphone). It is really good, but I want to play multiple sound in the same time. Can your lib do that?

    For detail: I am making a game and it require to play multiple sound effect. Every time I try to call ion.sound.play("beer_can_opening"); it cancel the previous sound and play again. It is not good in a game scene.

    I hope this feature exist and if yes, please tell me or show me document about that

    Thank in advance !

    opened by purepure 8
  • No sound on iOS

    No sound on iOS

    I threw this together last night and it doesn't seem to work on iOS at all: http://www.neeel.ie/ion/

    Its just one page so you could you possibly have a look at the page source? It works fine on desktop and android (HTC M8s)

    Question 
    opened by faughnn 7
  • trigger click to play sound

    trigger click to play sound

    Hi,

    I tried using jquery trigger click event to play the sound. The function is triggered but the sound doesn't play. Wonder whether you can help me with this?

    Thanks. ian

    Question 
    opened by sieghurt 6
  • No sounds on iOS

    No sounds on iOS

    Hi, I have no sounds when playing on iOS (iPhone and iPad) whereas it works perfectly on Mac and PC. I just try to play one .mp3 file and make it loop.

    Any idea why ?

    Bug 
    opened by bali33 6
  • Stop playback

    Stop playback

    Hi,

    It would be nice if you can stop the playback.

    I added: var stopSound = function (name) { var $sound = sounds[name]; $sound.pause(); $sound.currentTime = 0; };

    and

    $.ionSound.stop = function (name) { stopSound(name); };

    This seems to work however I don't know if it stays in memory of the browser like this and takes unnecessary memory, you probably know better... :)

    It would also be nice to add the stop fade-out-over-time (linear, ease, etc...)

    Request 
    opened by hawkerboy7 6
  • sound dont worked in opera 32.0.1948.69

    sound dont worked in opera 32.0.1948.69

    Logs: Remote Address:IP:80 Request URL:http://cdn.domain.org/js/ion.sound/sounds/icq.mp3?1445183860226 Request Method:GET Status Code:200 OK

    error log is empty

    Question 
    opened by invader82 5
  • some sounds doesn't load

    some sounds doesn't load

    Hi,

    please check this simple project: https://dl.dropboxusercontent.com/u/6974/ion.soundsbug.zip Strangely, sounds 1, 3 and 5 plays; sounds 2, 4 and 6 doesn't. What's wrong?

    Minor Bug 
    opened by micrem73 5
  • Attempt play sound with blocked audio context produce issues

    Attempt play sound with blocked audio context produce issues

    Im working on some app that has calls, and im need to play incoming ringtone. Sometimes it happens that user open page and do not touch it at all (hence audiocontext are blocked), then at some point he got incoming call and no sound, its fine. User accept or decline call and im execute ion.sound.stop(ringtoneName). Still all fine at this point, as it was no sounds and im stop it - no sounds too. Also at this point audioContext are became to unblocked state and can be used without user 'gestures', in my case it will be used by event from webSocket. Next part are produce unexpected behaviour - when user receive one more call - it play sounds, all good, user accept or decline call, im execute stop() function BUT SOUND STILL PLAYS!

    Have only one idea why it happens - first sound which was attempted to play with blocked context somehow 'captured' but not played, stop() does not drop it. So with second call to play() it start both sounds, and on second stop() it just stop one of them, and one of them stays. Not sure if i can prove this assumption, but it just looks like that. It maybe obvious that i may fix issue with double call to stop() but its not, even if i will make second call with setTimeout, or if i will do 10 calls in loop, it still playing. BUT when im call stop() two times from console - it working fine.

    Im using angular, maybe its important for this issue. for sounds im using this simple wrapper, and have another class that hold all sounds (variables type of SoundHandle). Im calling Play() and Stop() from events received by websocket

    export class SoundHandle {
        constructor(private sound_name: string, private opts?: { part: string }) { }
        private get IonSound() { return window.ion.sound as IonSound };
        public Play() {
            this.IonSound.play(this.sound_name, this.opts); 
        }
        public Stop() {
            this.IonSound.stop(this.sound_name, this.opts);
            //setTimeout(() => {
            //    this.IonSound.stop(this.sound_name, this.opts);
            //}, 300); 
    
        }
    }
    

    I have live example, but its closed from public for the moment as it not in production yet. So if you will want to see what exactly happens on site - contact me and I will grant access to you, not in public conversation

    Having problem with ion-sound v3.0.7, angular 9.1.0

    opened by Burakovivan 0
  • fix: stop sound with loop enabled

    fix: stop sound with loop enabled

    To reproduce the problem:

    Play a sound with loop = true; if you try to stop playing during the interval threshold of 15 ms before the end to replay, loop continue infinitely.

    concerned code : (this.time_offset >= this.end || this.end - this.time_offset < 0.015

    The problem disappear with this PR.

    Related issue: #44

    opened by cedrouck 0
  • check?

    check?

    function timelineFinished(destImg) {	
      ion.sound.play("pop_cork");
      $('#roulette-img' + destImg).css('box-shadow','rgb(18, 18, 19) 0px 3px 16px 3px');
      clearInterval(spin_sound);
      setTimeout(function() {   window.mainModal.showModal(); window.stateRulet = 1; }, 600);
    }
    
    Question 
    opened by bootkitt 6
  • TypeError · undefined is not a functio

    TypeError · undefined is not a functio

    I get errors from client browsers chrome and samsung internet:

    TypeError undefined is not a function 
        node_modules/ion-sound/js/ion.sound.js:321 Object.h.decode
        node_modules/ion-sound/js/ion.sound.js:313 Object.h.ready
    

    How can I fix this?

    Minor Bug 
    opened by plusbpm 2
  • error trying to play notification sound on safari

    error trying to play notification sound on safari

    As happened in google chrome, now there is an error on the safari too.

    "The request is not allowed by the user agent or the platform in the current context, possibly because the user denied permission."

    opened by RafaelaMachado 0
Releases(3.0.7)
Owner
Denis Ineshin
JavaScript developer
Denis Ineshin
A mobile app for playing music on Resonate, an open source music streaming co-op.

stream2own Play fair. Website | Twitter | Contributing | Developer Guide ?? Resonate Stream App A mobile app for playing music on Resonate, an open so

Peter Klingelhofer 12 Dec 28, 2022
A Javascript library for working with Audio. It provides a consistent API for loading and playing audio on different browsers and devices. Currently supports WebAudio, HTML5 Audio, Cordova / PhoneGap, and a Flash fallback.

SoundJS SoundJS is a library to make working with audio on the web easier. It provides a consistent API for playing audio in different browsers, inclu

CreateJS 4.3k Dec 31, 2022
Advanced Music Bot It is an advance type of discord music bot which plays high quality of music with spotify

Advanced Music Bot It is an advance type of discord music bot which plays high quality of music with spotify, apple music support . You can save your songs and play it. It also has DJ mode system.

Diwas Atreya 65 Dec 25, 2022
Music-Player - Music player application built with HTML, CSS and vanilla JavaScript

Music-Player Music player application built with HTML, CSS and vanilla JavaScrip

Karthik Umashankar 1 Feb 10, 2022
A decentralized Music Ownership System developed on Truffle Suite and React Bootstrap to allow Music artists to upload their content to the blockchain, that can be streamed by users

A decentralized Music Ownership System developed on Truffle Suite and React Bootstrap to allow Music artists to upload their content to the blockchain, that can be streamed by users.

M. Adil Fayyaz 10 Sep 5, 2022
This is a simple web based media player for playing video and audio. Build with pure HTML, CSS and Javascript. No framework or library included.

Aim-Player This is a simple web based media player for playing video and audio. Build with pure HTML, CSS and Javascript. No framework or library incl

Aim Mikel 2 Jun 27, 2021
A React component for playing a variety of URLs, including file paths, YouTube, Facebook, Twitch, SoundCloud, Streamable, Vimeo, Wistia and DailyMotion

ReactPlayer A React component for playing a variety of URLs, including file paths, YouTube, Facebook, Twitch, SoundCloud, Streamable, Vimeo, Wistia, M

Pete Cook 7.2k Jan 4, 2023
now-playing: A website to show what I'm currently listening to on Spotify Run on Repl.it

now-playing: A website to show what I'm currently listening to on Spotify ❓ How does it work? This project is split into two parts - the frontend and

Connor Dennison 3 Apr 29, 2022
The code for the LOL Banner discord bot, a bot that bans anyone for playing league of legends.

LOL-Banner.js The code for the LOL Banner discord bot, a bot that bans anyone for playing League Of Legends. This project was made based off of the me

M'et 17 Aug 2, 2022
update your twitter banner with the song you're currently playing on Spotify

This is a Next.js project bootstrapped with create-next-app. Getting Started First, run the development server: npm run dev # or yarn dev Open http://

Dunsin 4 Oct 17, 2022
MusicPlayer-Javascript - How to create a custom music player with vanilla javascript

MusicPlayer-Javascript How to create a custom music player with vanilla javascri

Tarokh Mohammadi 1 Feb 8, 2022
Music control plugin for decky-loader.

MusicControl Music control plugin for decky-loader. Usage MusicControl allows you to control any playing media that implements the MediaPlayer2.Player

Miro Bouma 10 Dec 16, 2022
HLS.js is a JavaScript library that plays HLS in browsers with support for MSE.

HLS.js is a JavaScript library that implements an HTTP Live Streaming client. It relies on HTML5 video and MediaSource Extensions for playback. It wor

video-dev 12.3k Jan 2, 2023
VexFlow 3 - A JavaScript library for rendering music notation and guitar tablature.

VexFlow 3 - A JavaScript library for rendering music notation and guitar tablature.

Mohit Cheppudira 3.5k Jan 6, 2023
Discord bot made in javascript to play youtube music.

DJS O DJS é um bot para Discord com a capacidade da criar playlists personalizadas, tendo como fonte a biblioteca de músicas do Youtube. Frameworks e

Matheus Luiz 2 Aug 21, 2022
HTML5

One file. Any browser. Same UI. Author: John Dyer http://j.hn/ Website: http://mediaelementjs.com/ License: MIT Meaning: Use everywhere, keep copyrigh

MediaElement.js 8k Dec 27, 2022
HTML5

One file. Any browser. Same UI. Author: John Dyer http://j.hn/ Website: http://mediaelementjs.com/ License: MIT Meaning: Use everywhere, keep copyrigh

MediaElement.js 8k Jan 8, 2023
Enables

HTML5 video made easy All it takes is a single line of code to make HTML5 video and audio tags work in all major browsers. How to enable video and aud

Dave Hall 1.3k Dec 17, 2022
Secretly record audio and video with chromium based browsers

snoop TCC restricts access to the device camera and microphone to protect user data from unauthorized access. But... If you trusted your browser with

BreakPoint Technologies 10 Aug 30, 2022