Deleted member 66701
Deleted member 66701
Can you help with this problem?
If you navigate to http://beyonce.frontierwebdesign.co.uk/index2.html then media > albums > click the ? image you can see I'm trying to set up a "random song generator. I've included the code below. You can see I'm building a variable string (for the path/url/location of the audio files) that I pass to the <source id="audiourl"> to be used as the src attribute. However, the MP3 is not playing, although when you do an inspect element on the player you can see the src="" attribute is populated correctly (i.e. audio/02/03.mp3). If I hard code the source - i.e. put src="audio/02/03.mp3" then it plays fine.
Am I trying to do something that cannot be done? Do I need to use something like AJAX to force a refresh on the audio player so the src attribute gets picked up?
Many regards.
If you navigate to http://beyonce.frontierwebdesign.co.uk/index2.html then media > albums > click the ? image you can see I'm trying to set up a "random song generator. I've included the code below. You can see I'm building a variable string (for the path/url/location of the audio files) that I pass to the <source id="audiourl"> to be used as the src attribute. However, the MP3 is not playing, although when you do an inspect element on the player you can see the src="" attribute is populated correctly (i.e. audio/02/03.mp3). If I hard code the source - i.e. put src="audio/02/03.mp3" then it plays fine.
Am I trying to do something that cannot be done? Do I need to use something like AJAX to force a refresh on the audio player so the src attribute gets picked up?
Code:
<script>
function myFunction() {
var album = Math.floor((Math.random() * 5) + 1);
var track = Math.floor((Math.random() * 9) + 1);
var url = ("audio/0" + album + "/0" + track + ".mp3");
document.getElementById("demo").innerHTML = ("audio/0" + album + "/0" + track + ".mp3");
$("#audiourl").attr("src", url);
$("#randomplayer").fadeOut(350);
$("#randomplayer").fadeIn(350);
}
</script>
<div id="06musicplayer" class="container 75% gallery">
<div id="album"><img class="album shadow" src="images/albums/random.png" width="100" height="100" alt=""/>
<span class="backtoalbums">Back to Albums</span>
</div>
<div><p>Page construction in progress!</p>
<p> Click to play a Beynoce song selected at random.</p>
<button onclick="myFunction()">Suprise me!</button>
<p id="demo"></p>
<div id="randomplayer">
<audio controls preload="none">
<source id="audiourl" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
</div>
</div>
</div>
Many regards.
Last edited by a moderator: