简体   繁体   中英

Changing source of HTML5 audio player with jquery

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>    

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM