X-Git-Url: http://git.dj3c1t.com/?p=mw_sourceml;a=blobdiff_plain;f=app%2Fout%2Fdefault%2Fjs%2Fjaudio-player.1.1.js;fp=app%2Fout%2Fdefault%2Fjs%2Fjaudio-player.1.1.js;h=e3e6ac06133a69ccd706d39416b467b8d71285f9;hp=0000000000000000000000000000000000000000;hb=8fd286a6a89f98742e6d8916622778df974a5bfd;hpb=64956f513749ece94e805784212825df60118d04 diff --git a/app/out/default/js/jaudio-player.1.1.js b/app/out/default/js/jaudio-player.1.1.js new file mode 100644 index 0000000..e3e6ac0 --- /dev/null +++ b/app/out/default/js/jaudio-player.1.1.js @@ -0,0 +1,368 @@ +/* + jaudioPlayer 1.1 + http://jaudio-player.dj3c1t.com/ + +*/ +(function($){ + + // ----------------------------------------------------------------------- + // fonction d'appel du plugin + + $.fn.jaudioPlayer = function(param){ + if(jap_api[param]) return jap_api[param].apply(this, Array.prototype.slice.call(arguments, 1)); + if(typeof param === 'object' || !param) return jap.init.apply(this, arguments); + }; + + // ----------------------------------------------------------------------- + // attributs + + var settings = {}, + audio_elts = {}, + audio_elt_index = 0, + waiting_track = false, + current_track = false, + seek_to_precent = 0; + + // ----------------------------------------------------------------------- + // fonctions publiques + + var jap_api = { + + set_option: function(option_name, value){ + settings[option_name] = value; + }, + + get_option: function(option_name){ + return settings[option_name]; + }, + + resize: function(){ + jap.resize_audio_players(); + }, + + clean_audio_elts: function(){ + for(var i in audio_elts){ + if($("#" + audio_elts[i].attr("id")).size() == 0){ + if(current_track == i){ + try{ + jap.stop(); + } + catch(e){} + current_track = false; + } + delete audio_elts[i]; + } + } + } + + }; + + // ----------------------------------------------------------------------- + // fonctions internes + + var jap = { + + init: function(options){ + settings = $.extend( + { + "waveform_class": "", + "player_graphics": "jaudio-player/jap-graphics.png", + "loading_img": "jaudio-player/jap-loading.gif", + "auto_play_next_track": false, + "loop": false + }, + options + ); + return this.each( + function(){ + var audio_elt = $(this); + if(audio_elt.is("audio")){ + jap.init_player(audio_elt); + } + } + ); + }, + + // ------------------- initialisation + + init_player: function(audio_elt){ + audio_elt_index++; + audio_elts[audio_elt_index] = audio_elt; + var CAN_PLAY = false; + audio_elt.find("source").each( + function(){ + if(audio_elt.get(0).canPlayType($(this).attr("type"))) CAN_PLAY = true; + } + ); + if(CAN_PLAY){ + audio_elt.wrap('
'); + $("#audio_wrapper_" + audio_elt_index).append( + "
" + + " " + + "
" + + " pause" + + " play" + + " stop" + + "
" + + "
" + + "
" + + "
" + + "
" + + "
" + + "
" + + " 00:00" + + " 00:00" + + "
" + + "
" + ); + if(settings["waveform_class"].length){ + if($("#audio_wrapper_" + audio_elt_index + " ." + settings["waveform_class"]).size()){ + $("#audio_wrapper_" + audio_elt_index).addClass("with_waveform"); + $("#audio_wrapper_" + audio_elt_index + " .player_progress div").html( + "" + ); + } + } + audio_elt.bind('loadedmetadata', function(e){ jap.track_loadedmetadata(e); }) + audio_elt.bind('progress', function(){ jap.track_progress(); }) + audio_elt.bind('canplaythrough', function(){ jap.track_canplaythrough(); }) + audio_elt.bind('timeupdate', function(){ jap.track_timeupdate(); }) + audio_elt.bind('waiting', function(){ jap.track_waiting(); }) + audio_elt.bind('playing', function(){ jap.track_playing(); }) + audio_elt.bind('ended', function(){ jap.track_ended(); }) + $("#audio_wrapper_" + audio_elt_index + " .player_controls a").css("background-image", "url(" + settings["player_graphics"] + ")"); + $("#audio_wrapper_" + audio_elt_index + " .player_controls .play").css("display: block"); + $("#audio_wrapper_" + audio_elt_index + " .player_controls .play").click(function() { jap.play($(this)); return false; }); + $("#audio_wrapper_" + audio_elt_index + " .player_controls .pause").click(function() { jap.pause(); return false; }); + $("#audio_wrapper_" + audio_elt_index + " .player_controls .stop").click(function() { jap.stop(); return false; }); + $("#audio_wrapper_" + audio_elt_index + " .player_progress").click(function(e) { jap.seek(e); }); + jap.resize_audio_players(); + } + }, + + resize_audio_players: function(){ + $(".audio_player .player_progress").each( + function(){ + var progress_width = $(this).width(); + $(this).find("img").css("width", progress_width); + } + ); + }, + + // ------------------- evenements + + track_loadedmetadata: function(e){ + var current_audio = e.target; + var minutes = "" + Math.floor(current_audio.duration / 60); + var secondes = "" + Math.round(current_audio.duration - (minutes * 60)); + if(minutes.length == 1) minutes = "0" + minutes; + if(secondes.length == 1) secondes = "0" + secondes; + $(current_audio).parents(".audio_wrapper").find(".time .duration").html(minutes + ":" + secondes); + }, + + track_progress: function(){ + if(current_track != false){ + var current_audio = audio_elts[current_track].get(0); + if(current_audio.buffered.length){ + var buffered = current_audio.buffered.end(0); + var loaded = parseInt(((buffered / current_audio.duration) * 100), 10); + } + $("#audio_player_" + current_track).find(".loaded").css("width", loaded + "%"); + } + }, + + track_canplaythrough: function(){ + if(waiting_track != false){ + current_track = waiting_track; + waiting_track = false; + setTimeout( + function(){ + var current_audio = audio_elts[current_track].get(0); + current_audio.play(); + jap.gui_state("playing"); + if(seek_to_precent != 0){ + current_audio.currentTime = (current_audio.duration / 100) * seek_to_precent; + seek_to_precent = 0; + } + }, + 500 + ); + } + }, + + track_timeupdate: function(){ + if(current_track != false){ + var current_audio = audio_elts[current_track].get(0); + var progress_bar = $("#audio_player_" + current_track).find(".position").get(0); + if(current_audio && progress_bar){ + progress_bar.style.width = ((100 * current_audio.currentTime) / current_audio.duration) + "%"; + var minutes = "" + Math.floor(current_audio.currentTime / 60); + var secondes = "" + Math.round(current_audio.currentTime - (minutes * 60)); + if(minutes.length == 1) minutes = "0" + minutes; + if(secondes.length == 1) secondes = "0" + secondes; + $("#audio_player_" + current_track + " .time .position").html(minutes + ":" + secondes); + var minutes = "" + Math.floor(current_audio.duration / 60); + var secondes = "" + Math.round(current_audio.duration - (minutes * 60)); + if(minutes.length == 1) minutes = "0" + minutes; + if(secondes.length == 1) secondes = "0" + secondes; + $("#audio_player_" + current_track + " .time .duration").html(minutes + ":" + secondes); + } + } + }, + + track_waiting: function(){ + jap.gui_state("waiting"); + }, + + track_playing: function(){ + jap.gui_state("playing"); + }, + + track_ended: function(){ + if(current_track != false){ + var current_audio = audio_elts[current_track].get(0); + jap.gui_state("stoped"); + if(settings["auto_play_next_track"] || settings["loop"]) jap.play_next_track(); + } + }, + + play_next_track: function(){ + var next_track = false, + first_track = false, + CURRENT_FOUND = false; + for(var track in audio_elts){ + if(!first_track) first_track = track; + if(track == current_track) CURRENT_FOUND = true; + else{ + if(CURRENT_FOUND){ + next_track = track; + break; + } + } + } + if(!next_track && settings["loop"] && first_track) next_track = first_track; + if(next_track){ + if(current_track != false) jap.stop(); + current_track = next_track; + jap.gui_state("waiting"); + current_track = false; + var current_audio = audio_elts[track].get(0); + waiting_track = next_track; + seek_to_precent = 0; + jap.load_track(current_audio); + } + }, + + load_track: function(audio){ + audio.preload = "auto"; + if(audio.readyState == audio.HAVE_ENOUGH_DATA) jap.track_canplaythrough(); + else audio.load(); + }, + + // ------------------- boutons + + play: function(play_elt){ + var track = play_elt.parents(".audio_wrapper").attr("id").substr(14); + if(current_track == track){ + if(audio_elts[track].get(0).paused){ + audio_elts[track].get(0).play(); + } + } + else{ + if(current_track != false) jap.stop(); + current_track = track; + jap.gui_state("waiting"); + current_track = false; + var current_audio = audio_elts[track].get(0); + waiting_track = track; + seek_to_precent = 0; + jap.load_track(current_audio); + } + }, + + seek: function(e){ + var $target = $(e.target); + if(true || $target.is("img")){ + var audio_player_elt = $target.parents(".audio_player"); + var track = audio_player_elt.attr("id").substr(13); + var progress_bar = audio_player_elt.find(".player_progress"); + var current_audio = audio_elts[track].get(0); + seek_to_precent = ((e.pageX - progress_bar.offset().left) * 100) / progress_bar.width(); + if(track){ + if(current_track == false || current_track != track){ + if(current_track != false) jap.stop(); + current_track = track; + jap.gui_state("waiting"); + current_track = false; + waiting_track = track; + jap.load_track(current_audio); + } + else{ + current_audio.currentTime = (current_audio.duration / 100) * seek_to_precent; + } + } + } + }, + + pause: function(){ + if(current_track != false){ + audio_elts[current_track].get(0).pause(); + jap.gui_state("paused"); + } + }, + + stop: function(){ + if(current_track != false){ + var current_audio = audio_elts[current_track].get(0); + current_audio.pause(); + if(current_audio.currentTime) current_audio.currentTime = 0; + jap.gui_state("stoped"); + current_track = false; + } + }, + + // ------------------- interface + + gui_state: function(state){ + if(current_track != false){ + jap.gui_blur(); + if(state == "waiting"){ + $("#audio_player_" + current_track).find(".play").css("display", "none"); + $("#audio_player_" + current_track).find(".pause").css("display", "none"); + $("#audio_player_" + current_track).find(".stop").css("display", "none"); + $("#audio_player_" + current_track).find("img.loading").css("display", "block"); + } + if(state == "playing"){ + $("#audio_player_" + current_track).find("img.loading").css("display", "none"); + $("#audio_player_" + current_track).find(".play").css("display", "none"); + $("#audio_player_" + current_track).find(".pause").css("display", "inline"); + $("#audio_player_" + current_track).find(".stop").css("display", "inline"); + } + else if(state == "paused"){ + $("#audio_player_" + current_track).find("img.loading").css("display", "none"); + $("#audio_player_" + current_track).find(".play").css("display", "inline"); + $("#audio_player_" + current_track).find(".pause").css("display", "none"); + $("#audio_player_" + current_track).find(".stop").css("display", "inline"); + } + else if(state == "stoped"){ + $("#audio_player_" + current_track).find("img.loading").css("display", "none"); + $("#audio_player_" + current_track).find(".play").css("display", "inline"); + $("#audio_player_" + current_track).find(".pause").css("display", "none"); + $("#audio_player_" + current_track).find(".stop").css("display", "none"); + $("#audio_player_" + current_track).find(".position").css("width", "0%"); + $("#audio_player_" + current_track).find(".time .position").html("00:00"); + } + } + }, + + gui_blur: function(){ + if(current_track != false){ + $("#audio_player_" + current_track).find(".play").get(0).blur(); + $("#audio_player_" + current_track).find(".pause").get(0).blur(); + $("#audio_player_" + current_track).find(".stop").get(0).blur(); + } + } + + }; + +})(jQuery); \ No newline at end of file