i wrote a joomla module for a webradio site, it's a player that reads files from a folder and plays them
this player has an on/off button for the radio; the problem is this:
i press on and everything is ok: the radio sings
i press off, ok: the radio stops
at this point if we press on again the radio sings but with unpredictable results: it skips songs or repeats them several times.
taking a look with firebug I see that the flow of the program is no longer regular but it seems to have gone crazy from there obviously the errors
in practice it only works well the first time, in fact if after the stop I reload the page everything proceeds well
this is the module
Code: Select all
<?php
function writemusic ($path) {
$dir='Unknown';
$arr=[];
foreach (new DirectoryIterator($path) as $fileInfo) {
if ($fileInfo->isDot()) continue;
if ($fileInfo->isDir()) {
$dir1=$fileInfo->getFilename();
foreach (new DirectoryIterator($path.$dir1.DIRECTORY_SEPARATOR) as $fileInfo) {
if ($fileInfo->isDot()) continue;
if ($fileInfo->getFilename() != '') {
array_push($arr, $dir1.'~'.$fileInfo->getFilename());
}
}
}
if ($fileInfo->getFilename() != '') {
array_push($arr, $dir.'~'.$fileInfo->getFilename());
}
}
$json = json_encode($arr);
file_put_contents($path.'music.json', $json);
}
function getname ($s) {
$a = explode('~', $s);
if ($a[0] == 'Unknown') {
$n = $a[1];
} else {
$n = $a[0].DIRECTORY_SEPARATOR.$a[1];
}
return ($n);
}
defined('_JEXEC') or die;
use Joomla\CMS\HTML\HTMLHelper;
$jpath = JPATH_SITE;
$jpath = rtrim($jpath, "/\\ \t\n\r\0\x0B");
$rpath = $params->get('music_folder').DIRECTORY_SEPARATOR;
$path = $jpath.DIRECTORY_SEPARATOR.$rpath;
if (!file_exists($path.'music.json')) {
writemusic($path);
}
$str = file_get_contents($path.'music.json');
$aaa = json_decode($str,true);
$arr=[];
foreach ($aaa as $item) {
$name=getname($item);
array_push($arr, JUri::root().$rpath.$name);
}
$accp = JUri::root().'images'.DIRECTORY_SEPARATOR.'ggg.png';
$spep = JUri::root().'images'.DIRECTORY_SEPARATOR.'eee.png';
?>
<div id="audio-player">
<div id="audio-info"><div class="scrolltext"><span id="title" class="title"></span>
</div>
</div>
<audio id="rvAudio">
<source src="">
</audio>
<div class="volume">
<label id="vol">Volume </label>
<input id="volume" class="volume" type="range" min="0" max="10" value="10">
</div>
<button title="Accendi la Vampira" id="autoplay-button"><img id="api" src="<?php echo $accp; ?>"></button>
<script>
var audioPlayer = document.getElementById("rvAudio");
var autoplayButton = document.getElementById("autoplay-button");
var volumeSlider = document.getElementById("volume");
var track = 0;
<?php echo "var jjj = ". json_encode($arr) . ";\n";
echo "var accp = ". json_encode("<img id=\"api\" src=\"$accp\">") . ";\n";
echo "var spep = ". json_encode("<img id=\"api\" src=\"$spep\">") . ";\n";
echo "var rp = ". json_encode($rpath) . ";\n";
echo "var ds = ". json_encode(DIRECTORY_SEPARATOR) . ";\n"; ?>
var arr=[];
for (var i=0;i<jjj.length;i++) {arr[i]=i;}
var ooo=arr.slice();
var r=-1;
var l=arr.length;
var title = '';
var ars = '';
var arp = '';
var ttt = '';
function autoplayTracks() {
playTrack();
audioPlayer.addEventListener("ended", function() {
playTrack();
});
}
function playTrack() {
r=Math.floor((Math.random()*l));
track = arr[r];
arr.splice(r,1);
l=arr.length;
if (l == 0) {
arr=ooo.slice();
}
audioPlayer.currentTime = 0;
audioPlayer.src = jjj[track];
title = audioPlayer.src;
audioPlayer.play();
ars = title.split(ds);
arp = rp.split(ds);
if (ars[ars.length-2] == arp[arp.length-2]) {
title = 'Unknown - ' + ars[ars.length-1];
} else {
title = ars[ars.length-2] + ' - ' + ars[ars.length-1];
}
ttt=title.replace(/\.[^/.]+$/, "");
ttt=decodeURI(ttt);
if (document.getElementById("title") !== null) {
document.querySelector('.title').textContent = ttt;
}
}
function stopTrack() {
audioPlayer.pause();
audioPlayer.currentTime = 0;
audioPlayer.src = '';
}
autoplayButton.addEventListener("click", function() {
if (autoplayButton.innerHTML == accp) {
autoplayButton.innerHTML = spep;
autoplayButton.title = "Spegni la Vampira";
arr=ooo.slice();
r=-1;
l=arr.length;
autoplayTracks();
} else {
autoplayButton.innerHTML = accp;
autoplayButton.title = "Accendi la Vampira";
stopTrack();
location.reload();
}
});
volumeSlider.addEventListener("input", function() {audioPlayer.volume = this.value/10;});
</script>
i don't know what I should reset after the stop.