app.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Create a WaveSurfer instance
  2. var wavesurfer;
  3. // Init on DOM ready
  4. document.addEventListener('DOMContentLoaded', function () {
  5. wavesurfer = WaveSurfer.create({
  6. container: '#waveform',
  7. waveColor: '#428bca',
  8. progressColor: '#31708f',
  9. height: 120,
  10. barWidth: 3
  11. });
  12. });
  13. // Bind controls
  14. document.addEventListener('DOMContentLoaded', function () {
  15. var playPause = document.querySelector('#playPause');
  16. playPause.addEventListener('click', function () {
  17. wavesurfer.playPause();
  18. });
  19. // Toggle play/pause text
  20. wavesurfer.on('play', function () {
  21. document.querySelector('#play').style.display = 'none';
  22. document.querySelector('#pause').style.display = '';
  23. });
  24. wavesurfer.on('pause', function () {
  25. document.querySelector('#play').style.display = '';
  26. document.querySelector('#pause').style.display = 'none';
  27. });
  28. // The playlist links
  29. var links = document.querySelectorAll('#playlist a');
  30. var currentTrack = 0;
  31. // Load a track by index and highlight the corresponding link
  32. var setCurrentSong = function (index) {
  33. links[currentTrack].classList.remove('active');
  34. currentTrack = index;
  35. links[currentTrack].classList.add('active');
  36. wavesurfer.load(links[currentTrack].href);
  37. };
  38. // Load the track on click
  39. Array.prototype.forEach.call(links, function (link, index) {
  40. link.addEventListener('click', function (e) {
  41. e.preventDefault();
  42. setCurrentSong(index);
  43. });
  44. });
  45. // Play on audio load
  46. wavesurfer.on('ready', function () {
  47. wavesurfer.play();
  48. });
  49. // Go to the next track on finish
  50. wavesurfer.on('finish', function () {
  51. setCurrentSong((currentTrack + 1) % links.length);
  52. });
  53. // Load the first track
  54. setCurrentSong(currentTrack);
  55. });