1
I am developing a Soundboard, and in this code, each <audio>
is generated by record found in the database, and each of them has a volume controller, however, all controllers only work pro last <audio>
generated, I believe it is because each iteration overwrites the last id, some idea of how I can generate a volume bar for each track?
<?php
while($teste = $usr_idLogged_aTracks_r->fetch_assoc()) {
$trackId = $teste['atrack_id'];
$trackTitle = $teste['atrack_title'];
$trackPath = $teste['atrack_path'];
$trackPlaylist = $teste['atrack_playlistId']; ?>
<div class="col-md-6 col-sm-12 col-xs-12" >
<div class="x_panel">
<div class="x_title">
<h2><?php echo $trackTitle ?> <small> <?php echo $trackId ?> </small></h2>
<ul class="nav navbar-right panel_toolbox">
<li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><i class="fa fa-wrench"></i></a>
<ul class="dropdown-menu" role="menu">
<li><a href="#">Settings 1</a>
</li>
<li><a href="#">Settings 2</a>
</li>
</ul>
</li>
<li><a class="close-link"><i class="fa fa-close"></i></a>
</li>
</ul>
<div class="clearfix"></div>
</div>
<div class="x_content">
<div class="row">
<audio style=" width: 100%; max-width: 600px" id="<?php echo $trackId ?>" controls>
<source src="<?php echo $trackPath ?>" type="audio/mp3">
</audio>
<input id="vol-control" type="range" min="0" max="100" step="1" oninput="SetVolume(this.value)" onchange="SetVolume(this.value)"></input>
<button onclick="document.getElementById('<?php echo $trackId ?>').play()">Reproduzir o áudio</button>
<button onclick="document.getElementById('<?php echo $trackId ?>').pause()">Pausar o áudio</button>.
<script type="text/javascript">
window.SetVolume = function(val) {
var player = document.getElementById('<?php echo $trackId ?>');
console.log('Before: ' + player.volume);
player.volume = val / 100;
console.log('After: ' + player.volume);
}
</script>
</div>
</div>
</div>
</div>
<?php }
?>
Good! That’s right, I wasn’t able to paste the script out of the loop.
– Luann Sousa
Great! Tamo together. Ah! I made it with pure javascript, but with Jquery(if you know), it’s easy too and with much less code.
– Taffarel Xavier