wavplayer.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. function WavPlayer(pid, heartext) {
  2. this.pid = pid;
  3. this.hearText = heartext;
  4. this.SoundLen = 0;
  5. this.SoundReady = 0;
  6. this.SoundPos = 0;
  7. this.Last = undefined;
  8. this.State = "STOPPED";
  9. this.Timer = undefined;
  10. this.stoptext = undefined;
  11. this.obj = undefined;
  12. this.initCnt = 0;
  13. this.ids = new Array();
  14. this.player = undefined;
  15. this.getId = function(id) {
  16. var element = (id in this.ids ? this.ids[id] : (this.ids[id] = document.getElementById(id)));
  17. }
  18. this.log = function(msg) {
  19. var log = this.getId("JSLOG");
  20. if(log) log.innerHTML += "<br/>"+msg;
  21. }
  22. this.getPlayer = function() {
  23. if(this.player!=undefined) return this.player;
  24. var obj = document.getElementById(this.pid);
  25. if (!obj) return null;
  26. if (obj.doPlay) {
  27. this.player = obj;
  28. return obj;
  29. }
  30. for(i=0; i<obj.childNodes.length; i++) {
  31. var child = obj.childNodes[i];
  32. if (child.tagName == "EMBED") {
  33. this.player = child;
  34. return child;
  35. }
  36. }
  37. }
  38. this.doPlay = function(obj) {
  39. this.log("doPlay("+obj.href+")");
  40. var player = this.getPlayer();
  41. this.doStop();
  42. this.obj = obj;
  43. this.stoptext = this.obj.innerHTML;
  44. this.obj.onclick = function(){ window.WavPlayer.doStop(); return false; }
  45. this.SoundPos = 0;
  46. this.SoundReady = 0;
  47. this.SoundLen = 0;
  48. player.doPlay(obj.href);
  49. }
  50. this.doStop = function () {
  51. this.log("doStop()");
  52. var player = this.getPlayer();
  53. player.doStop();
  54. }
  55. this.getPerc = function (a, b) {
  56. return ((b==0?0.0:a/b)*100).toFixed(2);
  57. }
  58. this.SoundLoad = function(secLoad, secTotal) {
  59. this.log("SoundLoad("+secLoad+","+secTotal+")");
  60. this.SoundReady = secLoad;
  61. this.SoundLen = secTotal;
  62. this.Inform();
  63. }
  64. this.Inform = function () {
  65. if (this.Last != undefined) {
  66. var now = new Date();
  67. var interval = (now.getTime()-this.Last.getTime())/1000;
  68. this.SoundPos += interval;
  69. this.Last = now;
  70. }
  71. this.log("Inform("+this.State+","+this.SoundPos+")");
  72. if (this.State=="STOPPED") {
  73. this.obj.innerHTML = this.stoptext;
  74. this.obj.onclick = (function(obj){ return function(){ window.WavPlayer.doPlay(obj); return false; } })(this.obj);
  75. this.Last = undefined;
  76. } else {
  77. this.obj.innerHTML = this.SoundPos.toFixed(2)+"/"+this.SoundReady.toFixed(2)+"/"+this.SoundLen.toFixed(2);
  78. }
  79. }
  80. this.SoundState = function (state, position) {
  81. this.log("SoundState("+this.State+" => "+state+")");
  82. if (position != undefined) this.SoundPos = position;
  83. if (this.State != "PLAYING" && state=="PLAYING") {
  84. this.Last = new Date();
  85. this.Timer = setInterval((function(t){ return function(){ t.Inform(); } })(this), 256);
  86. } else
  87. if (this.State == "PLAYING" && state!="PLAYING") {
  88. clearInterval(this.Timer);
  89. this.Timer = undefined;
  90. }
  91. this.State = state;
  92. this.Inform();
  93. }
  94. this.init = function () {
  95. var player = this.getPlayer();
  96. this.initCnt++;
  97. if (!player || !player.attachHandler) {
  98. if (this.initCnt < 50)
  99. setTimeout((function(t){ return function(){ return t.init(); } })(this), 100); // Wait for load
  100. } else {
  101. player.attachHandler("PLAYER_LOAD", "WavPlayerSoundLoad");
  102. player.attachHandler("PLAYER_BUFFERING", "WavPlayerSoundState", "BUFFERING");
  103. player.attachHandler("PLAYER_PLAYING", "WavPlayerSoundState", "PLAYING");
  104. player.attachHandler("PLAYER_STOPPED", "WavPlayerSoundState", "STOPPED");
  105. player.attachHandler("PLAYER_PAUSED", "WavPlayerSoundState", "PAUSED");
  106. //this.Inform();
  107. var as = document.body.getElementsByTagName("A");
  108. for(var i = 0; i<as.length; i++) if (as[i].href.match(/[.](al|alaw|au|gsm|raw|sln|ul|ulaw|wav|wav49)$/i)) {
  109. as[i].onclick = (function(obj){ return function(){ window.WavPlayer.doPlay(obj); return false; } })(as[i]);
  110. as[i].innerHTML = this.hearText;
  111. }
  112. var wavhead = this.getId('wavplayhead');
  113. if (wavhead) wavhead.innerHTML = "v:"+player.getVersion();
  114. }
  115. }
  116. }
  117. function WavPlayerSoundLoad() { window.WavPlayer.SoundLoad.apply(window.WavPlayer, arguments); }
  118. function WavPlayerSoundState() { window.WavPlayer.SoundState.apply(window.WavPlayer, arguments); }
  119. window.WavPlayer = new WavPlayer('WavPlayerBlock', "Прослушать");
  120. Event.domReady.add(function() {
  121. var Player = document.createElement("div");
  122. Player.style.display = "block";
  123. Player.setAttribute("id", "WavPlayerBlock");
  124. var attachPoint = document.body;
  125. var attachAnchor = undefined;
  126. var hs = document.getElementsByTagName('h3');
  127. if (hs.length == 1) {
  128. hs = hs[0];
  129. attachPoint = hs.parentElement ? hs.parentElement : hs.parentNode;
  130. attachAnchor = hs.nextSibling;
  131. }
  132. attachPoint.insertBefore(Player, attachAnchor);
  133. var vars = {}; var params = {'scale': 'noscale', 'bgcolor': '#FFFFFF'};
  134. swfobject.embedSWF("/ec/res/wavplayer.swf?gui=full&w=600&h=20", "WavPlayerBlock", "600", "20", "10.0.32.18", "/ec/res/expressInstall.swf", vars, params, params);
  135. window.WavPlayer.init();
  136. });