File: /home/imensosw/www/mpl.imenso.co/public/js/audio_player.js
var radioTimeout;
var audio_player = {
playlist: [],
current_song: 0,
state: 0,
player_showing: 0,
player: document.createElement("audio"),
progress_timeout: 0,
startRadioPlayer: function() {
var radio_data;
var first_track = true;
var radio_src = 'https://streaming02.zfast.co.uk/proxy/feedback?mp=/stream';
$(window).on('keydown', function(e) {
if(e.keyCode == 32)
{
var el = document.activeElement;
if(!(el && (el.tagName.toLowerCase() == 'input' && el.type == 'text' ||
el.tagName.toLowerCase() == 'textarea')))
{
e.preventDefault();
audio_player.toggle();
}
}
});
$.ajax({
url: '/ajax/get-current-playing-info',
success: function(data){
console.log(data)
radio_data = data;
$.ajax({
method: "POST",
url: url+"/ajax/get-artist-url",
data: {
artist_name: radio_data.artist,
_token: csrf_token
}
})
.done(function(response) {
if(JSON.parse(response) != ""){
$('.now-playing-row .album-cover').attr('href', '/artist/'+JSON.parse(response));
}
else
{
$('.now-playing-row .album-cover').removeAttr('href');
}
$('.now-playing-row .now-playing .song').text(radio_data.song);
// $('.now-playing-row .now-playing .artist').text(radio_data.artist);
// $('.now-playing-row .album-cover').css('background-image', 'url(\''+radio_data.cover+'\')');
if($('.now-playing-row .inner-name-scroll').width() > $('.track-name').width() && !$('.now-playing-row .inner-name-scroll').hasClass('toowide')){
$('.now-playing-row .inner-name-scroll').addClass('too-wide');
}
else
{
$('.now-playing-row .inner-name-scroll').removeClass('too-wide');
}
audio_player.addSong({
id: 0,
name: radio_data.title,
artist_name: radio_data.artist,
artist_url: JSON.parse(response),
src: radio_src,
image_src: radio_data.cover,
});
audio_player.setSong(0);
audio_player.startProgressInverval();
var promise = audio_player.player.play();
if (promise !== undefined) {
promise.catch(function(error) {
// Auto-play was prevented
// Show a UI element to let the user manually start playback
modal = $('[data-dialog="playRadio"]').remodal({
closeOnOutsideClick: false
});
modal.open();
}).then(function() {
audio_player.play();
});
}
// if (!Modernizr.touch && !$('html').hasClass('safari')) {
// audio_player.play();
// } else {
// modal = $('[data-dialog="playRadio"]').remodal({
// closeOnOutsideClick: false
// });
// modal.open();
// }
});
}
})
radioTimeout = setInterval(function(){
$.ajax({
url: '/ajax/get-current-playing-info',
success: function(data){
if(data.title != radio_data.title)
{
radio_data = data;
$.ajax({
method: "POST",
url: url+"/ajax/get-artist-url",
data: {
artist_name: radio_data.artist,
_token: csrf_token
}
})
.done(function(response) {
first_track = false;
if(JSON.parse(response) != ""){
$('.now-playing-row .album-cover').attr('href', '/artist/'+JSON.parse(response));
}
else
{
$('.now-playing-row .album-cover').removeAttr('href');
}
$('.now-playing-row .now-playing .song').text(radio_data.title);
// $('.now-playing-row .now-playing .artist').text(radio_data.artist);
// $('.now-playing-row .album-cover').css('background-image', 'url(\''+radio_data.cover+'\')');
if($('.now-playing-row .inner-name-scroll').width() > $('.track-name').width() && !$('.now-playing-row .inner-name-scroll').hasClass('toowide')){
$('.now-playing-row .inner-name-scroll').addClass('too-wide');
}
else
{
$('.now-playing-row .inner-name-scroll').removeClass('too-wide');
}
});
}
}
})
}, 5000);
},
toggle: function() {
if(this.state == 0)
{
this.play();
}
else
{
this.pause();
}
},
play: function(change_icon) {
if(!this.player_showing)
{
this.showPlayer();
}
if(typeof change_icon == 'undefined' || change_icon)
{
$('.audio-player').find('.controls .play-btn .fa').addClass('fa-pause').removeClass('fa-play');
}
this.startProgressInverval();
this.player.play()
this.state = 1;
},
pause: function(change_icon) {
if(typeof change_icon == 'undefined' || change_icon)
{
$('.audio-player').find('.controls .play-btn .fa').addClass('fa-play').removeClass('fa-pause');
}
clearInterval(this.progress_interval);
this.state = 0;
this.player.pause();
},
seekTo: function(time) {
if (!isNaN(this.player.duration)) {
this.player.currentTime = time;
}
},
next: function() {
this.pause();
this.seekTo(0);
this.updateSeekBar();
if(this.current_song == this.playlist.length-1)
{
this.current_song = 0;
}
else
{
this.current_song += 1;
}
this.setSong(this.playlist[this.current_song].id);
this.play();
},
prev: function() {
this.pause();
this.seekTo(0);
this.updateSeekBar();
if(this.current_song == 0)
{
this.current_song = this.playlist.length-1;
}
else
{
this.current_song -= 1;
}
this.setSong(this.playlist[this.current_song].id);
this.play();
},
addSong: function(song) {
this.playlist.push(song);
},
removeSong: function(id) {
for(x in this.playlist)
{
if(this.playlist[x].id == id)
{
this.playlist.splice(x, 1);
}
}
},
setSong: function(id) {
$('.song-row.playing').removeClass('playing');
this.seekTo(0);
for(x in this.playlist)
{
if(this.playlist[x].id == id)
{
this.current_song = parseInt(x);
this.player.src = this.playlist[x].src;
$('.audio-player').find('.song-details .image-holder').css('background-image', 'url(\''+this.playlist[x].image_src+'\')');
if(this.playlist[x].artist_url != ""){
$('.audio-player').find('.song-details .image-holder').attr('href', '/artist/'+this.playlist[x].artist_url);
}
else{
$('.audio-player').find('.song-details .image-holder').removeAttr('href');
}
$('.audio-player').find('.song-details .song-name').text(this.playlist[x].name);
$('.audio-player').find('.song-details .artist-name').text(this.playlist[x].artist_name);
}
}
this.player.load();
$('.song-row[data-song="'+id+'"]').addClass('playing');
},
playSong: function(id) {
this.hidePlayer();
this.removeSong(0);
this.setSong(id);
this.play();
},
showPlayer: function() {
this.player_showing = 1;
this.player.volume = 0.5;
$('.audio-player').addClass('active');
$(window).on('keydown', function(e) {
if(e.keyCode == 32 && !$('.audio-player').hasClass('radio'))
{
var el = document.activeElement;
if(!(el && (el.tagName.toLowerCase() == 'input' && el.type == 'text' ||
el.tagName.toLowerCase() == 'textarea')))
{
e.preventDefault();
audio_player.toggle();
}
}
});
},
hidePlayer: function() {
this.player_showing = 0;
this.pause();
if(radioTimeout != null){
clearTimeout(radioTimeout);
}
$('.audio-player').removeClass('active');
$('.audio-player').removeClass('radio');
},
updateSeekBar: function() {
$('.audio-player').find('.seek .progress-bar').css('width', this.getPercentagePlayed()+'%');
},
getPercentagePlayed: function() {
var currentTime = this.player.currentTime;
var duration = this.player.duration;
return (currentTime/duration)*100;
},
seekClick: function(e) {
if(!$(e.target).hasClass('progress-dot'))
{
var duration = this.player.duration;
var percentage = (e.clientX/(window.innerWidth-16))*100;
var seekTime = duration*(percentage/100);
this.seekTo(seekTime);
this.updateSeekBar();
}
else
{
clearInterval(this.progress_interval);
this.pause(false);
$('body').on('mousemove', function(e) {
var percentage = (e.clientX/(window.innerWidth-16))*100;
$('.audio-player').find('.seek .progress-bar').css('width', percentage+'%');
});
$('body').on('mouseup', function(e) {
var duration = audio_player.player.duration;
var percentage = (e.clientX/(window.innerWidth-16))*100;
var seekTime = duration*(percentage/100);
audio_player.seekTo(seekTime);
audio_player.play();
$('body').off('mousemove');
$('body').off('mouseup');
});
}
},
checkProgress: function() {
if(this.getPercentagePlayed() >= 100)
{
this.seekTo(0);
this.pause();
setTimeout(function() {
audio_player.next();
}, 1000);
}
},
startProgressInverval: function() {
this.progress_interval = setInterval(function() {
audio_player.updateSeekBar();
audio_player.checkProgress();
}, 10);
},
setVolume: function(e) {
$('body').off('mousemove');
var volume_bar = $('.audio-player').find('.volume-control .volume-bar');
var percentage = (e.clientX-volume_bar[0].getBoundingClientRect().left) / parseInt(volume_bar.css('width').replace('px','')) * 100;
var level = percentage/100;
if(level >= 0 && level <= 1)
{
$('.audio-player').find('.volume-control .volume-level').css('width', percentage+'%');
this.player.volume = level;
}
$('body').on('mousemove', function(e) {
audio_player.setVolume(e);
});
$('body').on('mouseup', function() {
$('body').off('mousemove');
$('body').off('mouseup');
});
},
changeVolume: function(amount) {
if(this.player.volume+amount < 0)
{
this.player.volume = 0;
}
else if(this.player.volume+amount > 1)
{
this.player.volume = 1;
}
else
{
this.player.volume += amount;
}
$('.audio-player').find('.volume-control .volume-level').css('width', (this.player.volume*100)+'%');
},
clearPlaylist: function() {
this.playlist = [];
}
};