Advertisement

the module only works well the first time Topic is solved

For Joomla! 5.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10.
Post Reply
romul
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Tue Feb 04, 2025 10:56 am

the module only works well the first time

Post by romul » Tue Feb 04, 2025 1:12 pm

hi
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 want to point out that the module if launched alone works perfectly, it is only when it is integrated with joomla that this happens
i don't know what I should reset after the stop.

Advertisement
romul
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Tue Feb 04, 2025 10:56 am

Re: the module only works well the first time

Post by romul » Wed Feb 05, 2025 8:46 pm

ok I solved it thanks to the help of the Italian Joomla forum
basically the problem was the line:

Code: Select all

	audioPlayer.addEventListener("ended", function() {
	playTrack();
	});
because it was accumulated with every click making a mess
now it is called only once and everything works
bye

User avatar
pe7er
Joomla! Master
Joomla! Master
Posts: 25403
Joined: Thu Aug 18, 2005 8:55 pm
Location: Nijmegen, Netherlands
Contact:

Re: the module only works well the first time

Post by pe7er » Wed Feb 05, 2025 9:33 pm

Welcome to Joomla forum!

Good to hear that you've solved it with help from the Italian Joomla community. Thanks for sharing your solution!
Kind Regards,
Peter Martin, Global Moderator + Joomla 5.2 Release Manager
Company website: https://db8.nl/en/ - Joomla specialist, Nijmegen, Netherlands
The best website: https://the-best-website.com

Advertisement

Post Reply

Return to “Joomla! 5.x Coding”