Jquery – Changing source of HTML5 audio player with jquery

html5-audiojquery

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()">&gt;</button>
</div>    

Best Answer

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.