Advertisement
Guest User

MP3 Player

a guest
Jul 30th, 2015
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Created by D_Pain
  2. // A small MP3 player you can add in your website.
  3.  
  4. // Create the audio tag
  5. var soundFile = document.createElement("audio");
  6. soundFile.preload = "auto";
  7.  
  8. // Load the sound file (using a source element for expandability)
  9. var src = document.createElement("source");
  10.     // Change this when swapping music!
  11. src.src = "YOUR_URL";
  12. soundFile.appendChild(src);
  13.  
  14. // Load the audio tag
  15. // It auto plays as a fallback
  16. soundFile.load();
  17. soundFile.volume = 1.000000;
  18.  
  19. // Plays the sound
  20. function play() {
  21.    // Set the current time for the audio file to the beginning
  22.    soundFile.currentTime = 0.01;
  23.    soundFile.volume = volume;
  24.  
  25.    // Due to a bug in Firefox, the audio needs to be played after a delay
  26.    setTimeout(function(){soundFile.play();},1);
  27. }
  28.  
  29. // Created by D_Pain
  30. // Creating element buttonPlay
  31. var e1 = document.createElement('div');
  32. e1.id = 'buttonPlay';
  33. e1.style.display = 'inline-block';
  34. e1.style.position = 'relative';
  35. e1.style.zIndex = '1';
  36. e1.style.backgroundImage = 'url(http://static.tumblr.com/cnaahls/Hk2nsbdeu/playshape.png)';
  37. e1.style.height = '25px';
  38. e1.style.width = '25px';
  39. e1.style.top = '0px';
  40. e1.style.left = '0px';
  41. e1.style.cursor = 'pointer';
  42.  
  43. // Creating element buttonStop
  44. var e2 = document.createElement('div');
  45. e2.id = 'buttonStop';
  46. e2.style.display = 'inline-block';
  47. e2.style.position = 'relative';
  48. e2.style.zIndex = '1';
  49. e2.style.backgroundImage = 'url(http://static.tumblr.com/cnaahls/nnknsbdfa/stopshape.png)';
  50. e2.style.height = '25px';
  51. e2.style.width = '25px';
  52. e2.style.top = '0px';
  53. e2.style.left = '5px';
  54. e2.style.cursor = 'pointer';
  55.  
  56. // Creating element buttonStop
  57. var e3 = document.createElement('div');
  58. e3.id = 'buttonVolUp';
  59. e3.style.display = 'inline-block';
  60. e3.style.position = 'relative';
  61. e3.style.zIndex = '1';
  62. e3.style.backgroundImage = 'url(http://static.tumblr.com/cnaahls/WeCnsbd0l/vol_.png)';
  63. e3.style.height = '25px';
  64. e3.style.width = '25px';
  65. e3.style.top = '0px';
  66. e3.style.left = '15px';
  67. e3.style.cursor = 'pointer';
  68.  
  69. // Creating element buttonStop
  70. var e4 = document.createElement('div');
  71. e4.id = 'buttonVolDown';
  72. e4.style.display = 'inline-block';
  73. e4.style.position = 'relative';
  74. e4.style.zIndex = '1';
  75. e4.style.backgroundImage = 'url(http://static.tumblr.com/cnaahls/RHlnsbd06/vol-.png)';
  76. e4.style.height = '25px';
  77. e4.style.width = '25px';
  78. e4.style.top = '0px';
  79. e4.style.left = '20px';
  80. e4.style.cursor = 'pointer';
  81.  
  82. // Creating element songName
  83. var e5 = document.createElement('marquee');
  84. e5.id = 'songName';
  85. e5.style.display = 'inline-block';
  86. e5.style.position = 'relative';
  87. e5.style.zIndex = '1';
  88. e5.style.width = '200px';
  89. e5.style.height = '17px';
  90. e5.style.top = '-4px';
  91. e5.style.left = '10px';
  92. e5.style.color = '#ffffff';
  93. e5.style.fontSize = '13px';
  94. e5.style.fontFamily = 'NanumGothic';
  95.     // Change this when swapping music!
  96. e5.innerHTML = 'DESCRIPTION OF THE MUSIC';
  97.  
  98. // Creating element mp3Player
  99. var container = document.createElement('div');
  100. container.id = 'mp3Player';
  101. container.style.position = 'fixed';
  102. container.style.top = '10px';
  103. container.style.left = '10px';
  104. container.style.height = '25px';
  105. container.style.width = '320px';
  106. container.style.backgroundColor = 'rgba(138, 138, 138, 0.3)';
  107. container.style.padding = '4px';
  108. container.appendChild(e1);
  109. container.appendChild(e2);
  110. container.appendChild(e5);
  111. container.appendChild(e3);
  112. container.appendChild(e4);
  113.  
  114. // Adding elements into body
  115. document.getElementsByTagName('body')[0].appendChild(container);
  116.  
  117. // Function to play sound
  118. function soundPlay() {
  119.   soundFile.play();
  120.   e5.start();
  121. }
  122.  
  123. // Function to stop sound
  124. function soundStop() {
  125.   soundFile.pause();
  126.   e5.stop();
  127. }
  128.  
  129. // Function to stop sound
  130. function soundVolUp() {
  131.   if(soundFile.volume<=1.000000){
  132.       soundFile.volume += 0.100000
  133.   }else{
  134.       soundFile.volume = 1.000000
  135.   }
  136. }
  137.  
  138. // Function to stop sound
  139. function soundVolDown() {
  140.   if(soundFile.volume>=0.000000){
  141.       soundFile.volume -= 0.100000
  142.   }else{
  143.       soundFile.volume = 0.000000
  144.   }
  145. }
  146.  
  147. // Adding event listeners
  148. e1.addEventListener("click", function(){soundPlay()}, false);
  149. e2.addEventListener("click", function(){soundStop()}, false);
  150. e3.addEventListener("click", function(){soundVolUp()}, false);
  151. e4.addEventListener("click", function(){soundVolDown()}, false);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement