What I’m trying to do is have it so that when the page loads have it play highpitchsound.wav (or .mp3) for x amount of seconds the user defines in an alertbox on page load.
In the alertbox there’s a textbox, and a button “Confirm”. The default text is 5 for 5s, or 5000 for 5000ms.
I’ve used intervals with jquery before, messed with audio in html5 a couple times here, and there, not sure how to combine the two for this, and also don’t know how I’d make a custom alertbox onload.
<html>
<head>
<title>Audio Play Interval</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
});
</script>
</head>
<body>
<audio autoplay="true">
<source src="sounds/highpitchsound.mp3" type="audio/mpeg" />
<source src="sounds/highpitchsound.wav" type="audio/wav" />
<source src="sounds/highpitchsound.ogg" type="audio/ogg" />
Your browser does not support the audio element.
</audio>
</body>
</html>
Understanding
You can achieve this by referencing the audio element specifically as an object. Because jquery does not extend
HTMLAudioElementyet you have to specifically refer to the first record within the jquery object for the audio element.You can then simply reference any of the functions such as
.play(),.pause()or.load()from then on.You can do this simply like so:
Breaking this down
We use
setInterval(a(),b)to run the functiona()everybamount of millisecondsreference the audio html object in the dom
$('audio')and then select theHTMLAudioElementobject within that to interact with it ($('audio')[0]).play())Solution
Now, in order to achieve what you want you can do this,
Javascript:
this first prompts the user to write in a time and then executes the function with the time set out by the user with a touch of validation.