Skip to content

Instantly share code, notes, and snippets.

@garrett
Created March 26, 2009 13:47
Show Gist options
  • Save garrett/86098 to your computer and use it in GitHub Desktop.
Save garrett/86098 to your computer and use it in GitHub Desktop.
/**
* from http://getsatisfaction.com/schillmania/topics/using_soundmanager_2_as_jquery_plugin
*/
$.playable = function( url, settings ) {
var sm = soundManager,
playable = arguments.callee;
sm.url = url;
sm.consoleOnly = window.location.hash.match(/console$/i);
sm.debugMode = window.location.hash.match(/^#debug/i);
$.extend( sm.defaultOptions,
{
autoStart : false,
autoNext : true,
pauseSkip : true,
playAlone : true,
doUnload : false,
css : {
playable: 'playable',
playing : 'playing',
paused : 'paused'
},
onload : function() {
if ( this.readyState == 2 )
playable.next.call( this );
},
onplay : function() {
var options = this.options,
current = options.element.removeClass( options.css.paused ).addClass( options.css.playing ).focus();
if ( playable.current && playable.current != current )
playable.current.data( 'playable' )[ options.pauseSkip ? 'pause' : 'stop' ]();
if ( options.playAlone )
playable.current = current;
},
onstop : function() {
var options = this.options;
options.element.removeClass( options.css.playing + ' ' + options.css.paused );
if ( options.doUnload )
this.unload();
},
onpause : function() {
var options = this.options;
options.element.removeClass( options.css.playing ).addClass( options.css.paused );
},
onresume : function() {
this.options.onplay.call( this );
},
onfinish : function() {
var options = this.options;
if ( options.autoNext )
playable.next.call( this );
options.onstop.call( this );
}
},
settings
);
$.extend(playable, {
count : 0,
current : null,
init : function( options ) {
var self = this,
options = $.extend( true, {}, sm.defaultOptions, options );
this.addClass( options.css.playable )
.click( function( event ) {
event.preventDefault();
$( this ).data( 'playable' ).togglePause();
} )
.each( function() {
$( this ).data( 'playable', sm.createSound( $.extend(
{
id : 'playable' + playable.count++,
url : this.href,
element : $( this ),
selector: self.selector
},
options
) ) );
} );
if ( options.autoStart )
self.filter( ':first' ).click();
},
next : function() {
var options = this.options,
next = $( options.element ).next( options.selector ).data( 'playable' );
if ( next && ! next.playState )
next.play();
}
});
};
$.fn.playable = function( options ) {
var self = this.is( 'a[href]' ) ? this : this.find( 'a[href]' );
soundManager.onload = function() {
if ( soundManager.canPlayURL( self.attr( 'href' ) ) )
$.playable.init.call( self, options );
};
};
$(function(){
/* Simple usage */
$.playable('soundmanager/swf/');
$('a').playable();
/* Full usage, with options
$.playable('soundmanager/swf/', { //options used here are stored as soundManager.defaultOptions
// You can use all SM2 default options, plus the following:
autoStart : false, // Start playing the first item
autoNext : false, // Play next itemwhen previous ends
pauseSkip : false, // Just pause previous on skip
playAlone : false, // Force stop/pause previous on skip
doUnload : false // Unload sound on stop/finish
});
//Example configurations
$('.playlist').playable({autoStart: true, autoNext: true, playAlone: true, doUnload: false});
$('.sampler').playable({autoStart: false, autoNext: false, playAlone: true});
$('.listen').playable({autoStart: false, autoNext: false, playAlone: true,pauseSkip: true, doUnload: true});
*/
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment