audio.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. var dot;
  2. $(function () {
  3. $('.toggle').click(function () {
  4. audio = $(this).parents('div').children('audio')[0]
  5. audio.addEventListener('timeupdate', updateProgress, false);
  6. audio.addEventListener('ended', audioEnded, false);
  7. if (audio.paused) {
  8. audio.play();
  9. $(this).parents('div').children('.controls').children('button').children().removeClass('glyphicon-play').addClass('glyphicon-pause')
  10. } else {
  11. audio.pause();
  12. $(this).parents('div').children('.controls').children('button').children().removeClass('glyphicon-pause').addClass('glyphicon-play')
  13. }
  14. })
  15. $('audio').on("loadedmetadata", function () {
  16. $(this).siblings('.controls').children('#audioTime').text(transTime(this.duration));
  17. var span = $(this).siblings('.pgs').children('span')
  18. if (!dot) {
  19. while (span.width() < span.parents('.panel-body').width()) {
  20. span.html(span.html() + '.')
  21. }
  22. dot = span.html().length
  23. } else {
  24. var s = ''
  25. for (let i = 0; i < dot; i++)s += '.'
  26. span.html(s)
  27. }
  28. });
  29. $('.pgs span').click(function (e) {
  30. var rate = (e.offsetX) / $(this).width();
  31. audio = $(this).parents('div').children('audio')[0]
  32. audio.currentTime = audio.duration * rate;
  33. var value = Math.floor((audio.currentTime / audio.duration) * dot - 0.01);
  34. var s = '';
  35. for (let i = 0; i < dot; i++)s += (i > value) ? '.' : ((i == value) ? '|' : '=')
  36. $(audio).siblings('.pgs').children('span').html(s)
  37. $(audio).siblings('.controls').children('.played-time').html(transTime(audio.currentTime));
  38. });
  39. window.onresize = resizeA
  40. })
  41. function resizeA() {
  42. var span = $($('.pgs>span')[0])
  43. span.html('')
  44. while (span.width() < span.parents('.panel-body').width()) {
  45. span.html(span.html() + '.')
  46. }
  47. dot = span.html().length
  48. $('.pgs>span').html(span.html())
  49. }
  50. function transTime(time) {
  51. var duration = parseInt(time);
  52. var minute = parseInt(duration / 60);
  53. var sec = duration % 60 + '';
  54. var isM0 = ':';
  55. if (minute == 0) minute = '00';
  56. else if (minute < 10) minute = '0' + minute;
  57. if (sec.length == 1) sec = '0' + sec;
  58. return minute + isM0 + sec
  59. }
  60. function updateProgress(e) {
  61. var value = Math.floor((this.currentTime / this.duration) * dot - 0.01);
  62. var s = '';
  63. for (let i = 0; i < dot; i++)s += (i > value) ? '.' : ((i == value) ? (this.paused?'|':'>') : '=')
  64. $(this).siblings('.pgs').children('span').html(s)
  65. $(this).siblings('.controls').children('.played-time').html(transTime(this.currentTime));
  66. }
  67. function audioEnded() {
  68. this.currentTime = 0;
  69. this.pause();
  70. $(this).parents('div').children('.controls').children('button').children().removeClass('glyphicon-pause').addClass('glyphicon-play')
  71. }