<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.6/styles/default.min.css">
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.6/styles/tomorrow.min.css">
<?php //$temp_data = ["Xa0Q0J5tOP0", "taJ60kskkns", "FG0fTKAqZ5g"] ?>
<style type="text/css">
.av_d_none{display: none;}
input[type=range] { margin-bottom: 30px; }
</style>
<div>
<p><span id="current-time">0:00</span> / <span id="duration">0:00</span> <span id="totalViewPercentage">0%</span></p>
<input type="range" id="progress-bar" value="0">
<br>
<?php foreach ($temp_data as $key => $value): ?>
<div id="video-placeholder_<?= $value ?>" data-id="<?= $value ?>" class="av_d_none"></div>
<?php endforeach ?>
</div>
<?php foreach ($temp_data as $key => $value): ?>
<div id="controls_<?= $value ?>" data-id="<?= $value ?>">
<p><span id="current-time_<?= $value ?>" data-id="<?= $value ?>">0:00</span> / <span id="duration_<?= $value ?>">0:00</span></p>
<input type="range" id="progress-bar_<?= $value ?>" data-id="<?= $value ?>" value="0">
<i id="play_<?= $value ?>" data-id="<?= $value ?>" class="material-icons">play_arrow</i>
<i id="pause_<?= $value ?>" data-id="<?= $value ?>" class="av_d_none material-icons">pause</i>
</div>
<?php endforeach ?>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.6/highlight.min.js"></script>
<script src="https://www.youtube.com/iframe_api"></script>
<script>
var player = [], time_update_interval = [], player_key = jQuery.parseJSON('<?= json_encode($temp_data) ?>');
function onYouTubeIframeAPIReady() {
for (var i = player_key.length - 1; i >= 0; i--) {
let temp_data2 = player_key[i];
player[player_key[i]] = new YT.Player('video-placeholder_'+player_key[i], {
width: 600,
height: 400,
videoId: player_key[i],
playerVars: {
color: 'white',
},
events: {
onReady: initialize,
onStateChange : function (event) {
if (event.data == 1) {
$('#play_'+temp_data2).click();
}
}
}
});
}
}
function initialize(){
for (var i = player_key.length - 1; i >= 0; i--) {
updateTimerDisplay(player_key[i]);
updateProgressBar(player_key[i]);
// $('#play_'+player_key[i]).css("display", "block");
$("#video-placeholder_"+player_key[0]).css("display", "block");
}
}
function updateTimerDisplay(player_key){
$('#current-time_'+player_key).text(formatTime( player[player_key].getCurrentTime() ));
$('#duration_'+player_key).text(formatTime( player[player_key].getDuration() ));
}
function updateProgressBar(player_key){
$('#progress-bar_'+player_key).val((player[player_key].getCurrentTime() / player[player_key].getDuration()) * 100);
}
function updateTotalProgressBar() {
player_key = jQuery.parseJSON('<?= json_encode($temp_data) ?>');
var totalCurrentTime = 0;
var totalDuration = 0;
for (var i = player_key.length - 1; i >= 0; i--) {
totalCurrentTime += player[player_key[i]].getCurrentTime();
totalDuration += player[player_key[i]].getDuration();
}
$('#progress-bar').val((totalCurrentTime / totalDuration) * 100);
$('#current-time').text(formatTime( totalCurrentTime ));
$('#duration').text(formatTime( totalDuration ));totalViewPercentage
$('#duration').text(formatTime( totalDuration ));
$('#totalViewPercentage').text(((totalCurrentTime / totalDuration) * 100).toFixed(2) +"%");
}
for (var i = player_key.length - 1; i >= 0; i--) {
$('#progress-bar_'+player_key[i]).on('mouseup touchend', function (e) {
var newTime = player[$(this).attr("data-id")].getDuration() * (e.target.value / 100);
player[$(this).attr("data-id")].seekTo(newTime);
});
$('#play_'+player_key[i]).on('click', function () {
var temp_data3 = '<?= json_encode($temp_data) ?>';
for (var i = player_key.length - 1; i >= 0; i--) {
if(player_key[i] != $(this).attr("data-id")){
$("#video-placeholder_"+player_key[i]).css("display", "none");
$("#pause_"+player_key[i]).click();
}
}
$("#video-placeholder_"+$(this).attr("data-id")).css("display", "block");
player[$(this).attr("data-id")].playVideo();
let temp_data = $(this).attr("data-id");
time_update_interval[$(this).attr("data-id")] = setInterval(function () {
updateTimerDisplay(temp_data);
updateProgressBar(temp_data);
updateTotalProgressBar();
}, 1000);
$("#play_"+$(this).attr("data-id")).css("display", "none");
$("#pause_"+$(this).attr("data-id")).css("display", "block");
});
$('#pause_'+player_key[i]).on('click', function () {
clearInterval(time_update_interval[$(this).attr("data-id")]);
player[$(this).attr("data-id")].pauseVideo();
$("#play_"+$(this).attr("data-id")).css("display", "block");
$("#pause_"+$(this).attr("data-id")).css("display", "none");
});
$('#mute-toggle_'+player_key[i]).on('click', function() {
var mute_toggle = $(this);
if(player[player_key[i]].isMuted()){
player[player_key[i]].unMute();
mute_toggle.text('volume_up');
}
else{
player[player_key[i]].mute();
mute_toggle.text('volume_off');
}
});
$('#volume-input_'+player_key[i]).on('change', function () {
player[player_key[i]].setVolume($(this).val());
});
$('#speed_'+player_key[i]).on('change', function () {
player[player_key[i]].setPlaybackRate($(this).val());
});
$('#quality_'+player_key[i]).on('change', function () {
player[player_key[i]].setPlaybackQuality($(this).val());
});
$('#next_'+player_key[i]).on('click', function () {
player[player_key[i]].nextVideo()
});
$('#prev_'+player_key[i]).on('click', function () {
player[player_key[i]].previousVideo()
});
$('.thumbnail_'+player_key[i]).on('click', function () {
var url = $(this).attr('data-video-id');
player[player_key[i]].cueVideoById(url);
});
}
function formatTime(time){
time = Math.round(time);
var minutes = Math.floor(time / 60),
seconds = time - minutes * 60;
seconds = seconds < 10 ? '0' + seconds : seconds;
return minutes + ":" + seconds;
}
</script>