[6] | 1 | |
---|
| 2 | $(document).ready |
---|
| 3 | ( function() |
---|
| 4 | { init_players(); |
---|
| 5 | init_player_listener(); |
---|
| 6 | } |
---|
| 7 | ); |
---|
| 8 | |
---|
| 9 | var current_track = false; |
---|
| 10 | |
---|
| 11 | function init_players() |
---|
| 12 | { $("audio").each |
---|
| 13 | ( function() |
---|
| 14 | { var audio_elt = $(this).get(0); |
---|
| 15 | var CAN_PLAY = false; |
---|
| 16 | var id_track = $(this).attr("id").substring(6); |
---|
| 17 | $(this).find("source").each |
---|
| 18 | ( function() |
---|
| 19 | { if(audio_elt.canPlayType($(this).attr("type"))) CAN_PLAY = true; |
---|
| 20 | } |
---|
| 21 | ); |
---|
| 22 | if(!CAN_PLAY) $("#track_" + id_track + " .player_controls").remove(); |
---|
| 23 | else |
---|
| 24 | { audio_elt.addEventListener("ended", track_ended, false); |
---|
| 25 | $("#player_controls_" + id_track + " .play").click(function() { play(id_track); return false; }); |
---|
| 26 | $("#player_controls_" + id_track + " .play").css("display: block"); |
---|
| 27 | $("#player_controls_" + id_track + " .pause").click(function() { pause(); return false; }); |
---|
| 28 | $("#player_controls_" + id_track + " .stop").click(function() { stop(); return false; }); |
---|
| 29 | } |
---|
| 30 | } |
---|
| 31 | ); |
---|
| 32 | } |
---|
| 33 | |
---|
| 34 | function track_ended() |
---|
| 35 | { gui_state("stoped"); |
---|
| 36 | var current_audio = $("#audio_" + current_track).get(0); |
---|
| 37 | if(current_audio.currentTime) current_audio.currentTime = 0; |
---|
| 38 | current_audio.pause(); |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | function track_progress() |
---|
| 42 | { if(current_track != false) |
---|
| 43 | { var current_audio = $("#audio_" + current_track).get(0); |
---|
| 44 | var position = Math.round((100 * current_audio.currentTime) / current_audio.duration); |
---|
| 45 | $("#track_" + current_track).find(".position").get(0).style.width = position + "%"; |
---|
| 46 | } |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | function init_player_listener() |
---|
| 50 | { setInterval("player_listener_update()", 300); |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | function player_listener_update() |
---|
| 54 | { if(current_track != false) |
---|
| 55 | { var current_audio = $("#audio_" + current_track).get(0); |
---|
| 56 | var position = Math.round((100 * current_audio.currentTime) / current_audio.duration); |
---|
| 57 | $("#track_" + current_track).find(".position").get(0).style.width = position + "%"; |
---|
| 58 | } |
---|
| 59 | }; |
---|
| 60 | |
---|
| 61 | function play(track) |
---|
| 62 | { if(current_track == track) |
---|
| 63 | { if($("#audio_" + current_track).get(0).paused) |
---|
| 64 | { $("#audio_" + current_track).get(0).play(); |
---|
| 65 | } |
---|
| 66 | } |
---|
| 67 | else |
---|
| 68 | { if(current_track != false) stop(); |
---|
| 69 | current_track = track; |
---|
| 70 | setTimeout("_play()", 1000); |
---|
| 71 | } |
---|
| 72 | gui_state("playing"); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | function _play() |
---|
| 76 | { if(current_track != false) |
---|
| 77 | { $("#audio_" + current_track).get(0).play(); |
---|
| 78 | } |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | function pause() |
---|
| 82 | { if(current_track != false) |
---|
| 83 | { $("#audio_" + current_track).get(0).pause(); |
---|
| 84 | gui_state("paused"); |
---|
| 85 | } |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | function stop() |
---|
| 89 | { if(current_track != false) |
---|
| 90 | { $("#audio_" + current_track).get(0).pause(); |
---|
| 91 | if($("#audio_" + current_track).get(0).currentTime) $("#audio_" + current_track).get(0).currentTime = 0; |
---|
| 92 | gui_state("stoped"); |
---|
| 93 | current_track = false; |
---|
| 94 | } |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | function gui_state(state) |
---|
| 98 | { gui_blur(); |
---|
| 99 | if(state == "playing") |
---|
| 100 | { $("#track_" + current_track).find(".play").get(0).style.display = "none"; |
---|
| 101 | $("#track_" + current_track).find(".pause").get(0).style.display = "inline"; |
---|
| 102 | $("#track_" + current_track).find(".stop").get(0).style.display = "inline"; |
---|
| 103 | $("#track_" + current_track).get(0).className = "playing_download"; |
---|
| 104 | |
---|
| 105 | } |
---|
| 106 | else if(state == "paused") |
---|
| 107 | { $("#track_" + current_track).find(".play").get(0).style.display = "inline"; |
---|
| 108 | $("#track_" + current_track).find(".pause").get(0).style.display = "none"; |
---|
| 109 | $("#track_" + current_track).find(".stop").get(0).style.display = "inline"; |
---|
| 110 | $("#track_" + current_track).get(0).className = "playing_download"; |
---|
| 111 | } |
---|
| 112 | else if(state == "stoped") |
---|
| 113 | { $("#track_" + current_track).find(".play").get(0).style.display = "inline"; |
---|
| 114 | $("#track_" + current_track).find(".pause").get(0).style.display = "none"; |
---|
| 115 | $("#track_" + current_track).find(".stop").get(0).style.display = "none"; |
---|
| 116 | $("#track_" + current_track).get(0).className = "download"; |
---|
| 117 | $("#track_" + current_track).find(".position").get(0).style.width = "0%"; |
---|
| 118 | } |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | function gui_blur() |
---|
| 122 | { $("#track_" + current_track).find(".play").get(0).blur(); |
---|
| 123 | $("#track_" + current_track).find(".pause").get(0).blur(); |
---|
| 124 | $("#track_" + current_track).find(".stop").get(0).blur(); |
---|
| 125 | } |
---|