bgworker.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. //Audio Module Background Worker
  2. //ArOZ Online BETA control system
  3. //Declare Global Variables
  4. var downloadmode = false;
  5. var lastactiveid = -1;
  6. var RepeatMode = 2;
  7. var SearchBarOn = false;
  8. var playingSong = [];
  9. ////////////////////////////
  10. $( document ).ready(function() {
  11. if (search_keyword != ""){
  12. //In Search Mode
  13. $( "#sbinput" ).val(search_keyword);
  14. SearchBarOn = true;
  15. }else{
  16. //Not in Search Mode
  17. $("#searchbar").hide();
  18. }
  19. $('#downloadmode_reminder').hide();
  20. $('#playbtn').attr('class', 'play icon');
  21. //Load the global volume from the stored data
  22. var globalvol = localStorage.getItem("global_volume");
  23. //console.log("Global Volume" + globalvol.toString());
  24. if (!globalvol){
  25. globalvol = 0.6;
  26. }
  27. player.volume = globalvol;
  28. $('#voldis').html(" " + (Math.round(player.volume * 100)).toString() + "%");
  29. //Load the repeat mode from stored data
  30. var repmode = localStorage.getItem("repeat_mode");
  31. if (repmode == 0){
  32. //0
  33. $('#repmode').html(' Single');
  34. RepeatMode = 0;
  35. }else if (repmode == 1){
  36. //1
  37. $('#repmode').html(' ALL');
  38. RepeatMode = 1;
  39. }else{
  40. //2
  41. $('#repmode').html(' None');
  42. RepeatMode = 2;
  43. }
  44. console.log(songlist);
  45. if (ShareMode){
  46. PlaySong(ShareSong[0],ShareSong[1],ShareSong[2]);
  47. window.setTimeout(CheckPlaying, 500);
  48. }
  49. });
  50. //On Pause or Play using Android notification on Firefox / Chrome
  51. var aud = document.getElementById("player");
  52. aud.onpause = function() {
  53. //Paused by the user on notification
  54. $('#playbtn').attr('class', 'play icon');
  55. };
  56. aud.onplay = function() {
  57. //Play by the user on notification
  58. $('#playbtn').attr('class', 'pause icon');
  59. };
  60. function toggleSearch(){
  61. //Id of Search Bar: searchbar
  62. if (SearchBarOn){
  63. //Need to close the Search Bar
  64. $("#searchbar").css("display", "none");
  65. SearchBarOn = false;
  66. }else{
  67. $("#searchbar").css("display", "block");
  68. SearchBarOn = true;
  69. $(window).scrollTop(0);
  70. $("#sbinput").focus();
  71. }
  72. }
  73. $('#searchbar').bind("enterKey",function(e){
  74. var keyword = $( "#sbinput" ).val();
  75. if (keyword != ""){
  76. window.location.href = "../Audio/index.php?search=" + keyword;
  77. }else{
  78. window.location.href = "../Audio/";
  79. }
  80. });
  81. $('#searchbar').keyup(function(e){
  82. if(e.keyCode == 13)
  83. {
  84. $(this).trigger("enterKey");
  85. }
  86. });
  87. function toggledownload(){
  88. if (downloadmode == true){
  89. $('#downloadmode_reminder').stop();
  90. $('#sbtext').html('Download Mode Disabled.');
  91. $('#downloadmode_reminder').fadeIn('slow').delay(2000).fadeOut('slow');
  92. downloadmode = false;
  93. }else{
  94. $('#downloadmode_reminder').stop();
  95. $('#sbtext').html('Download Mode Enabled.');
  96. $('#downloadmode_reminder').fadeIn('slow').delay(2000).fadeOut('slow');
  97. downloadmode = true;
  98. }
  99. }
  100. function getRealID(textid){
  101. return parseInt(textid.replace("AudioID",""));
  102. }
  103. function change(sourceUrl) {
  104. var audio = $("#player");
  105. $("#player").attr("src", "audio/" + sourceUrl);
  106. audio[0].pause();
  107. audio[0].load();//suspends and restores all audio element
  108. //audio[0].play(); changed based on Sprachprofi's comment below
  109. audio[0].oncanplaythrough = audio[0].play();
  110. }
  111. function share(){
  112. var shareSong = playingSong[0];
  113. var displayName = playingSong[1];
  114. var audioID = playingSong[2];
  115. if (shareSong != '' && shareSong != null){
  116. window.location.href="../QuickSend/index.php?share=" + "http://" + window.location.host + window.location.pathname + "?share=" + shareSong + "<and>display=" + displayName + "<and>id=" + audioID;
  117. } }
  118. function PlaySong(name,displayname,id){
  119. if (downloadmode == false){
  120. //This operation is for choosing song
  121. $('#songname').html('NOW PLAYING || '+ displayname);
  122. change(name);
  123. playingSong = [name,displayname,id];
  124. //console.log(playingSong);
  125. $('#playbtn').attr('class', 'pause icon');
  126. if (lastactiveid.toString() != id.toString()){
  127. $('#' + lastactiveid).attr('class', 'ts item');
  128. $('#' + id.toString()).attr('class', 'ts item active');
  129. }
  130. lastactiveid = id;
  131. //$(document).prop('title', displayname);
  132. //console.log(lastactiveid);
  133. $("#Audio").animate({ scrollTop: 0 }, "slow");
  134. }else if (downloadmode == true){
  135. //This operation is for downloading the audio file
  136. saveAs(name,displayname + ".mp3");
  137. }
  138. }
  139. function CheckPlaying(){
  140. var player = document.getElementById('player');
  141. if (player.paused){
  142. //Chrome does not allow instant playing of audio so user have to click the btn themselves.
  143. $('#playbtn').attr('class', 'play icon');
  144. }
  145. }
  146. function NextSong(){
  147. lastactiveid = parseInt(lastactiveid);
  148. if (lastactiveid != "undefined"){
  149. if (lastactiveid < songlist.length - 1){
  150. PlaySong(songlist[lastactiveid + 1][0],songlist[lastactiveid + 1][1],songlist[lastactiveid + 1][2]);
  151. }else{
  152. PlaySong(songlist[0][0],songlist[0][1],songlist[0][2]);
  153. }
  154. }
  155. }
  156. function PreviousSong(){
  157. lastactiveid = parseInt(lastactiveid);
  158. if (lastactiveid != "undefined"){
  159. if (lastactiveid > 0){
  160. PlaySong(songlist[lastactiveid - 1][0],songlist[lastactiveid - 1][1],songlist[lastactiveid - 1][2]);
  161. }
  162. }
  163. }
  164. function saveAs(uri, filename) {
  165. var link = document.createElement('a');
  166. if (typeof link.download === 'string') {
  167. document.body.appendChild(link); // Firefox requires the link to be in the body
  168. link.download = filename;
  169. link.href = uri;
  170. link.click();
  171. document.body.removeChild(link); // remove the link when done
  172. } else {
  173. location.replace(uri);
  174. }
  175. }
  176. function Show_Audio_Attrubute(){
  177. $("#audio_attr").fadeIn("slow");
  178. }
  179. function Hide_Audio_Attrubute(){
  180. $("#audio_attr").fadeOut("slow");
  181. }
  182. function repeatmode(){
  183. //This set the repeat mode for the browser reference.
  184. var repmode = localStorage.getItem("repeat_mode");
  185. // 0 = Single Repeat, repeat the same song
  186. // 1 = All Repeat, repeat when it reached the bottom of the list
  187. // 2 = No Repeat, stop after finishing this song.
  188. if (repmode == 0){
  189. //0 -> 1
  190. $('#repmode').html(' ALL');
  191. localStorage.setItem("repeat_mode", 1);
  192. }else if (repmode == 1){
  193. //1 -> 2
  194. $('#repmode').html(' None');
  195. localStorage.setItem("repeat_mode", 2);
  196. }else{
  197. //2 -> 0
  198. $('#repmode').html(' Single');
  199. localStorage.setItem("repeat_mode", 0);
  200. }
  201. }
  202. function playbtn(){
  203. if (document.getElementById('player').paused == true){
  204. $('#playbtn').attr('class', 'pause icon');
  205. $('#player').trigger('play');
  206. }else{
  207. $('#playbtn').attr('class', 'play icon');
  208. $('#player').trigger('pause');
  209. }
  210. }
  211. function stopbtn(){
  212. $('#player').trigger('pause');
  213. document.getElementById('player').currentTime = 0;
  214. $('#playbtn').attr('class', 'play icon');
  215. }
  216. $("#progressbardiv").click(function(e){
  217. var parentOffset = $(this).parent().offset();
  218. //or $(this).offset(); if you really just want the current element's offset
  219. var relX = e.pageX - parentOffset.left;
  220. var relY = e.pageY - parentOffset.top;
  221. var divwidth = $(this).parent().width();
  222. var ratio = relX / divwidth;
  223. var player = document.getElementById('player');
  224. var targettime = Math.round(player.duration * ratio);
  225. player.currentTime = targettime;
  226. //console.log(ratio);
  227. });
  228. var player = document.getElementById('player');
  229. //For every time update
  230. player.addEventListener("timeupdate", function() {
  231. var currentTime = player.currentTime;
  232. var duration = player.duration;
  233. $('#timecode').html(" " + FTF(Math.round(currentTime)) + "/" + FTF(Math.round(duration)));
  234. $('#audioprogress').stop(true,true).animate({'width':(currentTime)/duration*100+'%'},0,'linear');
  235. });
  236. //When the audio file is loaded
  237. player.addEventListener("canplay", function() {
  238. //Load the global volume from the stored data
  239. var globalvol = localStorage.getItem("global_volume");
  240. //console.log("Global Volume" + globalvol.toString());
  241. if (!globalvol){
  242. globalvol = 0.6;
  243. }
  244. player.volume = globalvol;
  245. $('#voldis').html(" " + (Math.round(player.volume * 100)).toString() + "%");
  246. //Load the repeat mode from stored data
  247. var repmode = localStorage.getItem("repeat_mode");
  248. if (repmode == 0){
  249. //0
  250. $('#repmode').html(' Single');
  251. RepeatMode = 0;
  252. }else if (repmode == 1){
  253. //1
  254. $('#repmode').html(' ALL');
  255. RepeatMode = 1;
  256. }else{
  257. //2
  258. $('#repmode').html(' None');
  259. RepeatMode = 2;
  260. }
  261. });
  262. //Event when the player finished the audio playing
  263. player.addEventListener("ended", function() {
  264. var repmode = localStorage.getItem("repeat_mode");
  265. if (repmode == 0){
  266. //0
  267. $('#repmode').html(' Single');
  268. RepeatMode = 0;
  269. }else if (repmode == 1){
  270. //1
  271. $('#repmode').html(' ALL');
  272. RepeatMode = 1;
  273. }else{
  274. //2
  275. $('#repmode').html(' None');
  276. RepeatMode = 2;
  277. }
  278. if (RepeatMode == 0){
  279. stopbtn();
  280. $('#player').trigger('play');
  281. }else if (RepeatMode == 1){
  282. NextSong();
  283. }else{
  284. stopbtn();
  285. }
  286. });
  287. function volUp(){
  288. var audio = document.getElementById("player");
  289. if (audio.volume > 0.9){
  290. audio.volume = 1;
  291. }else{
  292. audio.volume += 0.1;
  293. }
  294. $('#voldis').html(" " + (Math.round(audio.volume * 100)).toString() + "%");
  295. localStorage.setItem("global_volume", audio.volume);
  296. }
  297. function volDown(){
  298. var audio = document.getElementById("player");
  299. if (audio.volume < 0.1){
  300. audio.volume = 0;
  301. }else{
  302. audio.volume -= 0.1;
  303. }
  304. $('#voldis').html(" " + (Math.round(audio.volume * 100)).toString() + "%");
  305. localStorage.setItem("global_volume", audio.volume);
  306. }
  307. //Scroll Controller
  308. /*
  309. $(window).scroll(function (event) {
  310. var scroll = $(window).scrollTop();
  311. if (scroll > 200){
  312. $('#YamiPlayer').css('position', 'fixed');
  313. $('#YamiPlayer').css('top', '0');
  314. $('#YamiPlayer').css('right', '0');
  315. $('#YamiPlayer').css('width', '100%');
  316. $('#YamiPlayer').css('z-index', '1000');
  317. }else if (scroll < 50){
  318. $('#YamiPlayer').css('position', '');
  319. $('#YamiPlayer').css('top', '');
  320. $('#YamiPlayer').css('width', '100%');
  321. $('#YamiPlayer').css('right', '');
  322. $('#YamiPlayer').css('z-index', '');
  323. }
  324. });
  325. */
  326. function FTF(time)
  327. {
  328. // Hours, minutes and seconds
  329. var hrs = ~~(time / 3600);
  330. var mins = ~~((time % 3600) / 60);
  331. var secs = time % 60;
  332. // Output like "1:01" or "4:03:59" or "123:03:59"
  333. var ret = "";
  334. if (hrs > 0) {
  335. ret += "" + hrs + ":" + (mins < 10 ? "0" : "");
  336. }
  337. ret += "" + mins + ":" + (secs < 10 ? "0" : "");
  338. ret += "" + secs;
  339. return ret;
  340. }