app.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. var app = angular.module('ngWavesurfer', []);
  2. app.directive('ngWavesurfer', function () {
  3. return {
  4. restrict: 'E',
  5. link: function ($scope, $element, $attrs) {
  6. $element.css('display', 'block');
  7. var options = angular.extend({ container: $element[0] }, $attrs);
  8. var wavesurfer = WaveSurfer.create(options);
  9. if ($attrs.url) {
  10. wavesurfer.load($attrs.url, $attrs.data || null);
  11. }
  12. $scope.$emit('wavesurferInit', wavesurfer);
  13. }
  14. };
  15. });
  16. app.controller('PlaylistController', function ($scope) {
  17. var activeUrl = null;
  18. $scope.paused = true;
  19. $scope.$on('wavesurferInit', function (e, wavesurfer) {
  20. $scope.wavesurfer = wavesurfer;
  21. $scope.wavesurfer.on('play', function () {
  22. $scope.paused = false;
  23. });
  24. $scope.wavesurfer.on('pause', function () {
  25. $scope.paused = true;
  26. });
  27. $scope.wavesurfer.on('finish', function () {
  28. $scope.paused = true;
  29. $scope.wavesurfer.seekTo(0);
  30. $scope.$apply();
  31. });
  32. });
  33. $scope.play = function (url) {
  34. if (!$scope.wavesurfer) {
  35. return;
  36. }
  37. activeUrl = url;
  38. $scope.wavesurfer.once('ready', function () {
  39. $scope.wavesurfer.play();
  40. $scope.$apply();
  41. });
  42. $scope.wavesurfer.load(activeUrl);
  43. };
  44. $scope.isPlaying = function (url) {
  45. return url == activeUrl;
  46. };
  47. });