0

I am trying to play sound A when click button A, sound B when click B etc, in HTML5 audio, using javascript. I am working though the code and I am getting confused with my functions and vars. But I think the best way would be to have a function where playAudio(a) plays a.mp3 etc but I cant get it working.

The example below has the input ID defining the sound being played, but I cant make it dynamic.

Javascript

var a = sounds/a.mp3
var b = sounds/b.mp3

function playX() {
    var xAudio = document.getElementById('xxx');
         xAudio.src = $("#audiofile").val();          
    xAudio.play();   
}

HTML:

<input id="audiofile" type="text" value="sounds/a.mp3" /><br />
<input id="audiofileb" type="text" value="sounds/b.mp3" /><br />

<button id="play" onclick="playX();">        A    </button>
<button id="play" onclick="playX();">        B    </button>
<button id="play" onclick="playX();">        C   </button>



<audio id="xxx">  
</audio>

3 Answers 3

1
var a = sounds/a.mp3
var b = sounds/b.mp3

function playX(id) {
    var xAudio = document.getElementById('xxx');
         xAudio.src = $("#audiofile"+id).val();          
    xAudio.play();   
}

and html:

<input id="audiofileA" type="text" value="sounds/a.mp3" /><br />
<input id="audiofileB" type="text" value="sounds/b.mp3" /><br />
<input id="audiofileC" type="text" value="sounds/c.mp3" /><br />

<button id="play" onclick="playX('A');">        A    </button>
<button id="play" onclick="playX('B');">        B    </button>
<button id="play" onclick="playX('C');">        C   </button>
Sign up to request clarification or add additional context in comments.

1 Comment

yes - thanks. I was looking before the last edit and it wasnt working and didnt make sense. The edit works and I see how its working. Thanks (need 5 mins for tick)
0

There are a number of ways to do this.

Pass the filename as a parameter:

<button id="play" onclick="playX('sounds/a.mp3');">A</button>


function playX(file) {
    xAudio.src = file         
    xAudio.play();   
}

1 Comment

i think the code was using the audio tag to bring the html5 audio into play. This code doesnt work for me - maybe that is why?
0

If you use JQuery you can do something like this:

<button id="play" data-audiotrack="audiofile">        A    </button>
<button id="play" data-audiotrack="audiofileb">        B    </button>
<button id="play" data-audiotrack="audiofilec">        C   </button>

$('#play').click(function() {
    var audioTrack = $(this).data('audiotrack');
    var xAudio = document.getElementById(audioTrack);
    xAudio.src = $("#audiofile").val();          
    xAudio.play();
});

1 Comment

id prefer not to use jq for size, but there may be some benefits in this code so I may use it. Ill try it out thanks

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.