tinywav.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Pass ID of player's <object> tag, and length in second of pre-buffer
  2. function TinyWav(pid, trigger) {
  3. this.pid = pid;
  4. this.State = "STOPPED";
  5. this.initCnt = 0;
  6. this.player = undefined;
  7. this.playlist = [];
  8. this.trigger_buffer = trigger;
  9. this.doplaylist = false;
  10. // If one string passed --
  11. // Stop any current playback, clear playlist and run play of only file
  12. // If no argument passed --
  13. // Start/resume playback of playlist
  14. // If list passed --
  15. // replace playlist with it, and start playback of it
  16. this.Play = function(file) {
  17. var player = this.getPlayer();
  18. if (!file) {
  19. this.doplaylist = true;
  20. if (this.State != "STOPPED" || !this.playlist.length)
  21. return;
  22. file = this.playlist[0];
  23. } else
  24. if (typeof file == "object") {
  25. this.playlist = file;
  26. this.doplaylist = true;
  27. if (this.State != "STOPPED" || !this.playlist.length)
  28. return;
  29. file = this.playlist[0];
  30. } else {
  31. this.doplaylist = false;
  32. }
  33. this.Stop();
  34. player.doPlay(file, this.trigger_buffer);
  35. }
  36. // Add file(s) in playlist; does not starts playback
  37. this.Enqueue = function(file) {
  38. if (typeof file == "object") {
  39. this.playlist = this.playlist.concat(file);
  40. }
  41. else if (file) {
  42. if (!this.playlist || !this.playlist.length)
  43. this.playlist = [file];
  44. else
  45. this.playlist[this.playlist.length] = file;
  46. }
  47. }
  48. // Stop playback
  49. this.Stop = function () {
  50. var player = this.getPlayer();
  51. player.doStop();
  52. }
  53. // Pause playback
  54. this.Pause = function () {
  55. var player = this.getPlayer();
  56. player.doPause();
  57. }
  58. // Continue playback
  59. this.Resume = function () {
  60. var player = this.getPlayer();
  61. player.doResume();
  62. }
  63. // Advance to next playlist track
  64. this.Next = function() {
  65. var player = this.getPlayer();
  66. if(this.playlist.length) this.playlist.shift();
  67. if (!this.playlist.length)
  68. return;
  69. file = this.playlist[0];
  70. player.doStop();
  71. player.doPlay(file, this.trigger_buffer);
  72. }
  73. // ============= END OF API ==========
  74. // Find player object in page
  75. this.getPlayer = function() {
  76. if(this.player!=undefined) return this.player;
  77. var obj = document.getElementById(this.pid);
  78. if (!obj) return null;
  79. if (obj.doPlay) {
  80. this.player = obj;
  81. return obj;
  82. }
  83. for(i=0; i<obj.childNodes.length; i++) {
  84. var child = obj.childNodes[i];
  85. if (child.tagName == "EMBED") {
  86. this.player = child;
  87. return child;
  88. }
  89. }
  90. }
  91. this.SoundState = function (state, position) {
  92. if (position != undefined) this.SoundPos = position;
  93. if (this.State == "PLAYING" && state=="STOPPED" && this.doplaylist) {
  94. window.setTimeout((function(t){
  95. return function(){ t.Next(); };
  96. })(this), 50);
  97. }
  98. this.State = state;
  99. }
  100. this.init = function () {
  101. var player = this.getPlayer();
  102. this.initCnt++;
  103. if (!player || !player.attachHandler) {
  104. if (this.initCnt < 50)
  105. setTimeout((function(t){ return function(){ return t.init(); } })(this), 100); // Wait for load
  106. } else {
  107. player.attachHandler("PLAYER_BUFFERING", "TinyWavSoundState", "BUFFERING");
  108. player.attachHandler("PLAYER_PLAYING", "TinyWavSoundState", "PLAYING");
  109. player.attachHandler("PLAYER_STOPPED", "TinyWavSoundState", "STOPPED");
  110. player.attachHandler("PLAYER_PAUSED", "TinyWavSoundState", "PAUSED");
  111. }
  112. }
  113. }
  114. function TinyWavSoundState() { window.TinyWav.SoundState.apply(window.TinyWav, arguments); }
  115. window.TinyWav = new TinyWav('TinyWavBlock', 0.01);
  116. Event.domReady.add(function() {
  117. var Player = document.createElement("div");
  118. Player.style.display = "block";
  119. Player.setAttribute("id", "TinyWavBlock");
  120. document.body.appendChild(Player);
  121. var vars = {}; var params = {'scale': 'noscale', 'bgcolor': '#FFFFFF'};
  122. //swfobject.embedSWF("wavplayer-debug.swf?gui=none", "TinyWavBlock", "600", "300", "10.0.32.18", "embed/expressInstall.swf", vars, params, params);
  123. swfobject.embedSWF("modules/jsc/wavplay/wavplayer.swf?gui=none", "TinyWavBlock", "1", "1", "10.0.32.18", "modules/jsc/wavplay/embed/expressInstall.swf", vars, params, params);
  124. window.TinyWav.init();
  125. });