Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple usage of jquery.mb.audio #14

Open
pejman-hkh opened this issue Jun 15, 2016 · 0 comments
Open

Simple usage of jquery.mb.audio #14

pejman-hkh opened this issue Jun 15, 2016 · 0 comments

Comments

@pejman-hkh
Copy link

pejman-hkh commented Jun 15, 2016

Hello,
I writed a jQuery plugin for simple usage of jquery.mb.audio.
I hope that was usefull for you.

(function(window, $) {
    $.fn.mbAudio = function( options ) {

        var default_opts = {
            ended : function() {},
            pause: function() {},
            started : function() {},
            sprite: "",
        }

        var opts = $.extend(default_opts, options);

        $.mbAudio.sounds = {

        };

        selector =  this.selector;
        return $("body").on("click", selector, function() {

            if( typeof $(this).data('src') === 'undefined') {
                return false;
            }

            var name = 'file_'+$(selector).index( this );

            if( typeof $.mbAudio.sounds[name] === 'undefined' ) {
                var object_data = { id : name, mp3 : $(this).data('src'), elm : this };
                $.mbAudio.sounds[name] = object_data;
            }

            if( typeof $(this).data('played') !== 'undefined' ) {
                $.mbAudio.pause( name );
                $(this).removeData( 'played' );
                opts.pause( this );
                return false;
            }

            $(this).data( 'played', true );
            opts.started( this );

            var sprite = $( this ).data('sprite') ? $(this).data('sprite'): "";

            $.mbAudio.play( name, sprite, function( a, id ) {
                opts.ended( id.elm );
                $( id.elm ).removeData( 'played' );
             });   

            return false;
        });
    }

})(window, jQuery);

$(function() {
    $("a.audio-player").mbAudio({
        ended : function( a ) {
            console.log( 'ended' );
        },
        pause : function( a ) {
            console.log( 'pause' );
        },
        started : function( a ) {
            console.log( 'started' );
        }
    });

});
<a href="#" data-src="sounds/test.wav" data-sprite='{"id": "intro", "start": 10, "end": 20, "loop": true}' class="audio-player">test1</a>
<a href="#" data-src="sounds/test.wav" data-sprite="" class="audio-player">test2</a>

Also i changed plugin in line 214 to :

callback(player, $.mbAudio.sounds[sID] );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant