players.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. //云手机事件的相关逻辑 aac解码
  2. console.log("走到DGPlayer1");
  3. function DGPlayer(root) {
  4. console.log("走到DGPlayer2");
  5. var events = {},
  6. state = 'paused';
  7. new Image().src = "/dgplayer/resources/playbutton_active.png";
  8. new Image().src = "/dgplayer/resources/pausebutton.png";
  9. new Image().src = "/dgplayer/resources/pausebutton_active.png";
  10. new Image().src = "/dgplayer/resources/choosefile_pressed.png";
  11. // 阻止IE中的文本选择
  12. root.onselectstart = function() {
  13. return false
  14. }
  15. var seekBar = (function() {
  16. var loading = root.querySelector(".seek .track .loaded"),
  17. progress = root.querySelector(".seek .track .progress"),
  18. played = root.querySelector(".seek span:first-child"),
  19. remaining = root.querySelector(".seek span:last-child"),
  20. maxWidth = loading.parentNode.offsetWidth - 2,
  21. loaded = 0, currentTime = 0, trackLength = 0, oldSeconds = 0;
  22. function pad(input) {
  23. return ("00" + input).slice(-2);
  24. }
  25. return {
  26. getTrackLength: function() {
  27. return trackLength;
  28. },
  29. setTrackLength: function(time) {
  30. trackLength = time;
  31. this.seekTime = currentTime;
  32. },
  33. getCurrentTime: function() {
  34. return currentTime;
  35. },
  36. setCurrentTime: function(time) {
  37. oldSeconds = Math.floor(currentTime / 1000 % 60);;
  38. currentTime = time;
  39. if (currentTime >= trackLength && trackLength > 0)
  40. emit("pause");
  41. var t = currentTime / 1000,
  42. seconds = Math.floor(t % 60),
  43. minutes = Math.floor((t /= 60) % 60);
  44. if (seconds === oldSeconds)
  45. return;
  46. played.innerHTML = minutes + ':' + pad(seconds);
  47. // 如果我们知道持续时间,只显示进度条和剩余时间
  48. if (trackLength > 0) {
  49. var r = (trackLength - currentTime) / 1000,
  50. remainingSeconds = Math.floor(r % 60),
  51. remainingMinutes = Math.floor((r /= 60) % 60);
  52. remaining.innerHTML = '-' + remainingMinutes + ':' + pad(remainingSeconds);
  53. position = Math.max(0, Math.min(1, currentTime / trackLength));
  54. progress.style.width = maxWidth * position + 'px';
  55. } else {
  56. remaining.innerHTML = '-0:00';
  57. }
  58. },
  59. getAmountLoaded: function() {
  60. return loaded;
  61. },
  62. setAmountLoaded: function(val) {
  63. loaded = Math.max(0, Math.min(100, val));
  64. loading.style.width = maxWidth * (loaded / 100) + 'px';
  65. }
  66. }
  67. })();
  68. var playpause = (function() {
  69. var button = root.querySelector(".button"),
  70. interval = null, playing = false;
  71. button.onclick = function() {
  72. emit(playing ? "pause" : "play");
  73. };
  74. root.addEventListener('keyup', function(e) {
  75. e.which === 32 && emit(playing ? "pause" : "play");
  76. });
  77. function setPlaying(play) {
  78. if (playing = play)
  79. button.classList.add("pause");
  80. else
  81. button.classList.remove("pause");
  82. }
  83. return {
  84. setPlaying: setPlaying,
  85. getPlaying: function() {
  86. return playing;
  87. }
  88. }
  89. })();
  90. var slider = (function() {
  91. var handle = root.querySelector(".volume .handle"),
  92. progress = root.querySelector(".volume .progress"),
  93. track = root.querySelector(".volume .track")
  94. volumeLeft = root.querySelector(".volume img:first-child"),
  95. volumeRight = root.querySelector(".volume img:last-child");
  96. var lastY = 0,
  97. down = false,
  98. height = 65,
  99. handleSize = 20,
  100. min = -8,
  101. max = height - handleSize / 2 - 3,
  102. curY = Math.floor(height / 2 - handleSize / 2),
  103. value = 50;
  104. function update(dontEmit) {
  105. if ('webkitTransform' in handle.style)
  106. handle.style.webkitTransform = "translate3d(0, " + (-max - min + curY) + "px" + ", 0)";
  107. else
  108. handle.style.bottom = max + min - curY + "px";
  109. progress.style.height = max - curY + "px";
  110. value = Math.round(100 - (curY - min) / (max - min) * 100);
  111. if (!dontEmit)
  112. emit("volume", value);
  113. }
  114. update();
  115. handle.onmousedown = handle.ontouchstart = function(e) {
  116. lastY = e.targetTouches ? e.targetTouches[0].pageY : e.clientY;
  117. down = true;
  118. e.stopPropagation();
  119. handle.classList.add("active");
  120. e.preventDefault();
  121. }
  122. function onMove(e) {
  123. var eventY = e.targetTouches ? e.targetTouches[0].pageY : e.clientY;
  124. var y = Math.max(min, Math.min(max, curY + eventY - lastY));
  125. if (!down || y === curY) return;
  126. curY = y;
  127. lastY = eventY;
  128. update();
  129. }
  130. function onUp(e) {
  131. if(!down) return;
  132. down = false;
  133. handle.classList.remove("active");
  134. }
  135. document.addEventListener("mousemove", onMove, false);
  136. document.addEventListener("touchmove", onMove, false);
  137. document.addEventListener("mouseup", onUp, false);
  138. document.addEventListener("touchend", onUp, false);
  139. // Handle clicking on the minimum and maximum volume icons
  140. function animate() {
  141. handle.classList.add("animatable");
  142. progress.classList.add("animatable");
  143. update();
  144. setTimeout(function() {
  145. handle.classList.remove("animatable");
  146. progress.classList.remove("animatable");
  147. }, 250);
  148. }
  149. volumeLeft.onclick = function() {
  150. curY = min;
  151. animate();
  152. }
  153. volumeRight.onclick = function() {
  154. curY = max;
  155. animate();
  156. }
  157. // 控制点击轨迹
  158. track.onmousedown = track.ontouchstart = function(e) {
  159. var y = e.targetTouches ? e.targetTouches[0].pageY : e.clientY;
  160. // 得到轨道的绝对偏移量
  161. var offset = 0, obj = track;
  162. while (obj) {
  163. offset += obj.offsetTop - obj.scrollTop;
  164. obj = obj.offsetParent;
  165. }
  166. curY = Math.max(min, Math.min(max, y - offset - (handleSize + min)));
  167. handle.onmousedown(e);
  168. update();
  169. }
  170. return {
  171. getValue: function() {
  172. return value;
  173. },
  174. setValue: function(val) {
  175. val = Math.max(0, Math.min(100, val));
  176. curY = max - (val / 100) * (max - min);
  177. update(true);
  178. }
  179. }
  180. })();
  181. var filebutton = (function() {
  182. var button = root.querySelector('.file_button'),
  183. input = document.createElement('input');
  184. input.setAttribute('type', 'file');
  185. input.style.opacity = 0;
  186. input.style.position = 'absolute';
  187. input.style.left = '-1000px';
  188. input.onchange = function(e) {
  189. emit('file', input.files[0]);
  190. }
  191. button.onclick = function() {
  192. input.click();
  193. console.log("bbbbbbbbbbbbb");
  194. }
  195. })();
  196. function emit(event) {
  197. if (!events[event]) return;
  198. var args = Array.prototype.slice.call(arguments, 1),
  199. callbacks = events[event];
  200. for (var i = 0, len = callbacks.length; i < len; i++) {
  201. callbacks[i].apply(null, args);
  202. }
  203. }
  204. var API = {
  205. on: function(event, fn) {
  206. events[event] || (events[event] = []);
  207. events[event].push(fn);
  208. },
  209. off: function(event, fn) {
  210. var eventsOf = events[event],
  211. index = eventsOf.indexOf(fn);
  212. ~index && eventsOf.splice(index, 1);
  213. }
  214. };
  215. // Object.defineProperty Opera垫片
  216. Object.defineProperty || (Object.defineProperty = function(obj, prop, config) {
  217. if (config.get && obj.__defineGetter__)
  218. obj.__defineGetter__(prop, config.get);
  219. if (config.set && obj.__defineSetter__)
  220. obj.__defineSetter__(prop, config.set);
  221. })
  222. Object.defineProperty(API, "bufferProgress", {
  223. get: seekBar.getAmountLoaded,
  224. set: seekBar.setAmountLoaded
  225. });
  226. Object.defineProperty(API, "state", {
  227. set: function(newstate) {
  228. playpause.setPlaying(newstate == 'playing' || newstate == 'buffering');
  229. state = newstate;
  230. },
  231. get: function() {
  232. return state;
  233. }
  234. });
  235. Object.defineProperty(API, "duration", {
  236. get: seekBar.getTrackLength,
  237. set: seekBar.setTrackLength
  238. });
  239. Object.defineProperty(API, "seekTime", {
  240. get: seekBar.getCurrentTime,
  241. set: seekBar.setCurrentTime
  242. });
  243. Object.defineProperty(API, "volume", {
  244. get: slider.getValue,
  245. set: slider.setValue
  246. });
  247. var img = root.querySelector(".avatar img");
  248. Object.defineProperty(API, "coverArt", {
  249. get: function() {
  250. return img.src;
  251. },
  252. set: function(src) {
  253. img.src = src;
  254. }
  255. });
  256. var title = root.querySelector("span.title"),
  257. artist = root.querySelector("span.artist");
  258. Object.defineProperty(API, "songTitle", {
  259. get: function() {
  260. return title.innerHTML;
  261. },
  262. set: function(val) {
  263. title.innerHTML = val;
  264. }
  265. });
  266. Object.defineProperty(API, "songArtist", {
  267. get: function() {
  268. return artist.innerHTML;
  269. },
  270. set: function(val) {
  271. artist.innerHTML = val;
  272. }
  273. });
  274. var description = root.querySelector('.file_description');
  275. Object.defineProperty(API, "fileDescription", {
  276. get: function() {
  277. return description.innerHTML;
  278. },
  279. set: function(val) {
  280. description.innerHTML = val;
  281. }
  282. });
  283. return API;
  284. }