2

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>    
3
  • 1
    posted a fiddle might help u in ur cause..jsbin.com/EDAxoLiL/1/edit Commented Oct 28, 2013 at 11:12
  • 1
    @codebreaker can i kiss you ?? :) you saved my day :) Commented Oct 28, 2013 at 11:42
  • 1
    Oh good.. the fiddle helped u solve ur task..happy coding.. Commented Oct 28, 2013 at 11:52

1 Answer 1

9

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.