Reputation: 361
I don't know why it doesn't work and i have spend alot of time on it
I want to change the source of an audio player and start playing the new source on a button click, here's the code:
JS:
<script type="text/javascript">
function audplay() {
var player = document.getElementById("Audio1");
audio = $("<audio>").attr("id", "audioElement")
.attr("preload", "auto")
.appendTo(player);
var sourceUrl = 'C:\Users\a\a\a\1.mp3'
function addMp3Source(sourceUrl) {
audio.empty();
var newSrc = $("<source>").attr("src",sourceUrl).appendTo(audio);
} }
</script>
HTML:
<audio id="Audio1" controls="controls" >
<source id='a' src="Miaow-07-Bubble.m4a" type="audio/mp4" />
<source src="demo.ogv" type="video/ogg" />
HTML5 Audio is required for this example.
<div id="buttonbar">
<button id="play" onclick="audplay()">></button>
</div>
Upvotes: 2
Views: 9264
Reputation: 2725
try this:
HTML:
<audio controls id="Audio1"></audio>
jQuery (write below code on any button click):
$("#play").on("click",function(){
var fileName = "dummy.mp3";
$("#Audio1").attr("src",fileName).trigger("play");
});
Don't need to write separate source tags.
Upvotes: 9