Hi, Today I am talking about a Javascript sound API (soundManager 2) which supports MP3, MPEG4, and HTML5 Audio. It helps us to play any audio in the easiest way.
it’s 100% Flash free MP3 + MP4/AAC (and OOG, FLAC etc) supported.
Compatible with Apple iPad (iOS 3.2), iPhone/iOS 4 and newer.
Basic API Features
- Load, stop, play, pause, mute, seek, pan (Flash-only) and volume control of sounds from JavaScript
- Events:
onload
,whileloading
,whileplaying
,onfinish
and more.
Implementation:-
Just add the soundManager2.js file in your HTML code.
<script src=”soundmanager2.js”></script>
Sounds can be created with instance-specific parameters in an object literal (JSON-style) format, where omitted parameters inherit default values as defined in soundManager.
1 2 3 4 5 6 7 8 9 10 |
soundManager.createSound({ id: 'mySound', url: '/path/to/some.mp3', autoLoad: true, autoPlay: false, onload: function() { alert('The sound '+this.id+' loaded!'); }, volume: 50 }); |
This object can also be passed as an optional argument to the play
method, overriding options set at creation time.
Object Method:-
SoundManager provides wrappers for most SMSound methods – eg. soundManager.play('mySound')
checks for a valid sound object, and then calls soundManager.sounds['mySound'].play()
on that particular object; thus, it is the same as var sound = soundManager.getSoundById('mySound'); mySound.play();
The following methods can be called directly on a SMSound instance. The method calls are the same as the SoundManager global methods documented above for existing sound objects, minus the sound ID parameter.
SoundManager: Default Sound Options:-
Sound objects inherit properties from soundManager.defaultOptions
when they are created.
Sound parameters can be overridden on a per-sound basis at create time, or changed dynamically in some cases. Note that none of these options are required when calling createSound()
except for id
and url
; the others will inherit the default values if unspecified.
soundManager.defaultOptions
apply the properties and event handlers as specified above. Defaults are shown below as an example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
soundManager.defaultOptions = { autoLoad: false, // enable automatic loading (otherwise .load() will call with .play()) autoPlay: false, // enable playing of file ASAP (much faster if "stream" is true) from: null, // position to start playback within a sound (msec), see demo loops: 1, // number of times to play the sound. Related: looping (API demo) multiShot: true, // let sounds "restart" or "chorus" when played multiple times.. multiShotEvents: false, // allow events (onfinish()) to fire for each shot, if supported. onid3: null, // callback function for "ID3 data is added/available" onload: null, // callback function for "load finished" onstop: null, // callback for "user stop" onfinish: null, // callback function for "sound finished playing" onpause: null, // callback for "pause" onplay: null, // callback for "play" start onresume: null, // callback for "resume" (pause toggle) position: null, // offset (milliseconds) to seek to within downloaded sound. pan: 0, // "pan" settings, left-to-right, -100 to 100 stream: true, // allows playing before entire file has loaded (recommended) to: null, // position to end playback within a sound (msec), see demo type: null, // MIME-like hint for canPlay() tests, eg. 'audio/mp3' usePolicyFile: false, // enable crossdomain.xml request for remote domains (for ID3/waveform access) volume: 100, // self-explanatory. 0-100, the latter being the max. whileloading: null, // callback function for updating progress (X of Y bytes received) whileplaying: null, // callback during play (position update) // see optional flash 9-specific options, too } |
Some places that do or have used SM2 include SoundCloud, Tidal, Beats, Songza, freesound.org, last.fm, 8tracks, Discogs, and The Hype Machine among others.
I hope you like this blog and you can play audio in an easy way.
Please comments and share your experience with us to make us better next time.
Recent Comments