Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var path1 = "/music/music1.mp3";
- var Player = {
- media: null,
- mediaTimer: null,
- isPlaying: false,
- initMedia: function(path1) { //intitializing audio file according to path selected when clicked from list on files-ist.html
- Player.media = new Media(
- path1,
- function() {
- console.log('Media file read succesfully');
- if (Player.media !== null)
- Player.media.release(); //released to free resources since too many instances of the function will be heavy
- Player.resetLayout();
- },
- function(error) {
- navigator.notification.alert(
- 'Unable to read the media file.',
- function(){},
- 'Error'//error message if media not read
- );
- Player.changePlayButton('play');
- console.log('Unable to read the media file (Code): ' + error.code);
- }
- );
- },
- playPause: function(path1) {
- if (Player.media === null)
- Player.initMedia(path1); //reinitiate path if no media
- if (Player.isPlaying === false)
- {
- Player.media.play(); //if not playing, then play
- Player.mediaTimer = setInterval(
- function() {
- Player.media.getCurrentPosition( //this needs to be here to resume from pause
- function(position) {
- if (position > -1)
- {
- $('#media-played').text(Utility.formatTime(position));
- Player.updateSliderPosition(position);
- }
- },
- function(error) {
- console.log('Unable to retrieve media position: ' + error.code);
- $('#media-played').text(Utility.formatTime(0));
- }
- );
- },
- 1000
- );
- var counter = 0;
- var timerDuration = setInterval(
- function() {
- counter++;
- if (counter > 20)
- clearInterval(timerDuration);
- var duration = Player.media.getDuration();
- if (duration > -1)
- {
- clearInterval(timerDuration); //clear and start if audio has not yet started
- $('#media-duration').text(Utility.formatTime(duration)); //refer to utility.js
- $('#time-slider').attr('max', Math.round(duration));
- $('#time-slider').slider('refresh');
- }
- else
- $('#media-duration').text('Unknown');
- },
- 100
- );
- Player.changePlayButton('pause'); //change button to 'pause' when 'play' button is pressed
- }
- else
- {
- Player.media.pause();
- clearInterval(Player.mediaTimer);
- Player.changePlayButton('play'); //if paused change to 'play'
- }
- Player.isPlaying = !Player.isPlaying;
- },
- stop: function() {
- if (Player.media !== null)
- {
- Player.media.stop();
- Player.media.release(); //stop the audio and release to free resources
- }
- clearInterval(Player.mediaTimer);
- Player.media = null;
- Player.isPlaying = false;
- Player.resetLayout();
- },
- resetLayout: function() {
- $('#media-played').text(Utility.formatTime(0));
- Player.changePlayButton('play');
- Player.updateSliderPosition(0); //designate original position for slider and change pause to play if needed
- },
- updateSliderPosition: function(seconds) {
- var $slider = $('#time-slider');
- if (seconds < $slider.attr('min'))
- $slider.val($slider.attr('min'));
- else if (seconds > $slider.attr('max'))
- $slider.val($slider.attr('max'));
- else
- $slider.val(Math.round(seconds));
- $slider.slider('refresh'); //slider control. validates slider position value so that audio length matches with slider length
- },
- seekPosition: function(seconds) {
- if (Player.media === null)
- return;
- Player.media.seekTo(seconds * 1000); //convert milliseconds to seconds
- Player.updateSliderPosition(seconds);
- },
- changePlayButton: function(imageName) {
- var background = $('#player-play')
- .css('background-image')
- .replace('url(', '')
- .replace(')', '');
- $('#player-play').css(
- 'background-image',
- 'url(' + background.replace(/images\/.*\.png$/, 'images/' + imageName + '.png') + ')' //controls image for play/pause buttons
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement