/** * MI Player - custom audio player * 23.09.2016 **/ (function($,document,window) { var isTouch = 'ontouchstart' in window; canPlayType = function( file ) { var audioElement = document.createElement( 'audio' ); return !!( audioElement.canPlayType && audioElement.canPlayType( 'audio/' + file.split( '.' ).pop().toLowerCase() + ';' ).replace( /no/, '' ) ); }; $.fn.miPlayer = function(options) { // Establish our default settings var options = $.extend({ autoplay : true, volume : 50, playlist : null, metaDataFile : 'playlist.php', filetype : '.mp3', callback: function() { return true; }, }, options); var $player = $(this), $btnControl = $player.find('.btn-control'), $audio = $player.find('#media'), audio = $audio.get(0), audioFile = $audio.attr('src'); var $loading = $('
'); var $volumeBar = $player.find('.volume .bar'); var $muteBtn = $player.find('.volume .mute'); var $playlist = options.playlist ? $('#'+options.playlist) : null; var volumeCurrent = options.volume; var isSupported = false; $player.find('.control').prepend($loading); if( typeof audioFile !== 'undefined' && canPlayType(options.filetype)) isSupported = true; if(isTouch) isSupported = true; if (isSupported) { $audio.css( { 'width': 0, 'height': 0, 'visibility': 'hidden' } ); audio.addEventListener('loadeddata', function() { if(audio.readyState >= 2) { $loading.remove(); } }); if (options.autoplay) { audio.play(); } if (isPlaying(audio)) { $btnControl.html('').addClass('playing'); } else { audio.pause(); audio.src = ''; } /**Default Volume***/ setVolume(volumeCurrent); /**Volume**/ $volumeBar.slider({ min: 0, max: 100, value: volumeCurrent, range: "min", animate: true, slide: function(event, ui) { setVolume(ui.value); volumeCurrent = ui.value; audio.muted = false; $muteBtn.html('').removeClass('muted'); } }); /**MUTE BOUTON***/ $muteBtn.on('touchstart click', function(e){ e.preventDefault(); if ($(this).hasClass('muted')) { audio.muted = false; $(this).html('').removeClass('muted'); //setVolume(volumeCurrent); } else { audio.muted = true; $(this).html('').addClass('muted'); //setVolume(volumeCurrent); } }); /**Buton play/pause**/ $btnControl.on('touchstart click', function(e) { e.preventDefault(); if ($(this).hasClass('playing')) { audio.pause(); $(this).html('').removeClass('playing'); audio.src = 'about:blank'; } else { audio.src = audioFile; $(this).html('').addClass('playing'); audio.play(); } }); } else { $player.find('.volume').remove(); var $embed = $(''); $player.append($embed); } function setVolume(volume) { audio.volume = (volume/100); } function isPlaying(audioelem) { return !audioelem.paused; } /*** * GET DATA PLAYLIST AJAX ***/ function getPlaylist(){ $.get(options.metaDataFile, function(data) { if (data!='ERROR XML') { var content = $.parseJSON(data); if (content.currentsong) { $playlist.find('.current-item img').attr('src',content.currentsong.albumcover+'?rand='+Math.random()); $playlist.find('.current-item .song-title').html(content.currentsong.songtitle); $playlist.find('.current-item .artist').html(content.currentsong.artist); } /***NEXT ITEM***/ if (content.nextsong) { if ($playlist.find('.next-item').length) { $playlist.find('.next-item img').attr('src',content.nextsong.albumcover+'?rand='+Math.random()); $playlist.find('.next-item .song-title').html(content.nextsong.songtitle); $playlist.find('.next-item .artist').html(content.nextsong.artist); } else { var nextItem = $('

Prochain

vignette'+ '
'+content.nextsong.songtitle+'
'+content.nextsong.artist+'
'); $playlist.append(nextItem); } } /***PEVIOUS ITEM**/ if (content.lastsong) { if ($playlist.find('.prev-item').length) { $playlist.find('.prev-item img').attr('src',content.lastsong.albumcover+'?rand='+Math.random()); $playlist.find('.prev-item .song-title').html(content.lastsong.songtitle); $playlist.find('.prev-item .artist').html(content.lastsong.artist); } else { var prevItem = $('

Précédent

vignette'+ '
'+content.lastsong.songtitle+'
'+content.lastsong.artist+'
'); $playlist.append(prevItem); } } } setTimeout(function(){ getPlaylist(); }, 5000); }); } /** * Playlist */ if ($playlist) { var $plsCurrent = $('
vignette
On Air
Song title
Song artist
'); $playlist.html($plsCurrent); getPlaylist(); } }; })(jQuery,document,window);