main.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. 'use strict';
  2. // Create an instance
  3. var wavesurfer;
  4. // Init & load audio file
  5. document.addEventListener('DOMContentLoaded', function () {
  6. // Init
  7. wavesurfer = WaveSurfer.create({
  8. container: document.querySelector('#waveform'),
  9. waveColor: '#A8DBA8',
  10. progressColor: '#3B8686',
  11. backend: 'MediaElement',
  12. plugins: [
  13. WaveSurfer.regions.create({
  14. regions: [
  15. {
  16. start: 0,
  17. end: 5,
  18. color: 'hsla(400, 100%, 30%, 0.1)'
  19. }, {
  20. start: 10,
  21. end: 100,
  22. color: 'hsla(200, 50%, 70%, 0.1)'
  23. }
  24. ]
  25. }),
  26. WaveSurfer.timeline.create({
  27. container: '#timeline'
  28. })
  29. ]
  30. });
  31. // Load audio from URL
  32. wavesurfer.load('../media/demo.wav');
  33. // Zoom slider
  34. var slider = document.querySelector('[data-action="zoom"]');
  35. slider.value = wavesurfer.params.minPxPerSec;
  36. slider.min = wavesurfer.params.minPxPerSec;
  37. slider.addEventListener('input', function () {
  38. wavesurfer.zoom(Number(this.value));
  39. });
  40. // Play button
  41. var button = document.querySelector('[data-action="play"]');
  42. button.addEventListener('click', wavesurfer.playPause.bind(wavesurfer));
  43. });