Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 KB | None | 0 0
  1. var path1 = "/music/music1.mp3";
  2. var Player = {
  3. media: null,
  4. mediaTimer: null,
  5. isPlaying: false,
  6. initMedia: function(path1) { //intitializing audio file according to path selected when clicked from list on files-ist.html
  7. Player.media = new Media(
  8. path1,
  9. function() {
  10. console.log('Media file read succesfully');
  11. if (Player.media !== null)
  12. Player.media.release(); //released to free resources since too many instances of the function will be heavy
  13. Player.resetLayout();
  14. },
  15. function(error) {
  16. navigator.notification.alert(
  17. 'Unable to read the media file.',
  18. function(){},
  19. 'Error'//error message if media not read
  20. );
  21. Player.changePlayButton('play');
  22. console.log('Unable to read the media file (Code): ' + error.code);
  23. }
  24. );
  25. },
  26. playPause: function(path1) {
  27. if (Player.media === null)
  28. Player.initMedia(path1); //reinitiate path if no media
  29.  
  30. if (Player.isPlaying === false)
  31. {
  32. Player.media.play(); //if not playing, then play
  33. Player.mediaTimer = setInterval(
  34. function() {
  35. Player.media.getCurrentPosition( //this needs to be here to resume from pause
  36.  
  37. function(position) {
  38. if (position > -1)
  39. {
  40. $('#media-played').text(Utility.formatTime(position));
  41. Player.updateSliderPosition(position);
  42. }
  43. },
  44. function(error) {
  45. console.log('Unable to retrieve media position: ' + error.code);
  46. $('#media-played').text(Utility.formatTime(0));
  47. }
  48. );
  49. },
  50. 1000
  51. );
  52. var counter = 0;
  53. var timerDuration = setInterval(
  54. function() {
  55. counter++;
  56. if (counter > 20)
  57. clearInterval(timerDuration);
  58.  
  59. var duration = Player.media.getDuration();
  60. if (duration > -1)
  61. {
  62. clearInterval(timerDuration); //clear and start if audio has not yet started
  63. $('#media-duration').text(Utility.formatTime(duration)); //refer to utility.js
  64. $('#time-slider').attr('max', Math.round(duration));
  65. $('#time-slider').slider('refresh');
  66. }
  67. else
  68. $('#media-duration').text('Unknown');
  69. },
  70. 100
  71. );
  72.  
  73. Player.changePlayButton('pause'); //change button to 'pause' when 'play' button is pressed
  74. }
  75. else
  76. {
  77. Player.media.pause();
  78. clearInterval(Player.mediaTimer);
  79. Player.changePlayButton('play'); //if paused change to 'play'
  80. }
  81. Player.isPlaying = !Player.isPlaying;
  82. },
  83. stop: function() {
  84. if (Player.media !== null)
  85. {
  86. Player.media.stop();
  87. Player.media.release(); //stop the audio and release to free resources
  88. }
  89. clearInterval(Player.mediaTimer);
  90. Player.media = null;
  91. Player.isPlaying = false;
  92. Player.resetLayout();
  93. },
  94. resetLayout: function() {
  95. $('#media-played').text(Utility.formatTime(0));
  96. Player.changePlayButton('play');
  97. Player.updateSliderPosition(0); //designate original position for slider and change pause to play if needed
  98. },
  99. updateSliderPosition: function(seconds) {
  100. var $slider = $('#time-slider');
  101.  
  102. if (seconds < $slider.attr('min'))
  103. $slider.val($slider.attr('min'));
  104. else if (seconds > $slider.attr('max'))
  105. $slider.val($slider.attr('max'));
  106. else
  107. $slider.val(Math.round(seconds));
  108.  
  109. $slider.slider('refresh'); //slider control. validates slider position value so that audio length matches with slider length
  110. },
  111. seekPosition: function(seconds) {
  112. if (Player.media === null)
  113. return;
  114.  
  115. Player.media.seekTo(seconds * 1000); //convert milliseconds to seconds
  116. Player.updateSliderPosition(seconds);
  117. },
  118. changePlayButton: function(imageName) {
  119. var background = $('#player-play')
  120. .css('background-image')
  121. .replace('url(', '')
  122. .replace(')', '');
  123.  
  124. $('#player-play').css(
  125. 'background-image',
  126. 'url(' + background.replace(/images\/.*\.png$/, 'images/' + imageName + '.png') + ')' //controls image for play/pause buttons
  127. );
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement