In this example I want to show how using x.currentTime = 10; the audio takes the 10 second position and starts playing right from that point. My question is, how can you do the other way around, so that the audio ends 10 seconds earlier? Since I haven't found any documentation about it. Grateful in advance for your patience and wisdom.
var x = document.getElementById("myAudio");
function getCurTime() {
alert(x.currentTime);
}
function setCurTime() {
x.currentTime = 10;
}
<audio id="myAudio" controls>
<source src="https://freesound.org/data/previews/469/469273_4397472-lq.mp3" type="audio/mpeg">
</audio><br>
<button onclick="setCurTime()" type="button">Establecer la posición de inicio a 10 segundos</button>
The media elements (audio and video) have the attribute
duration
, which you can use to calculate what you want:On the other hand, you have the event
timeupdate
, which is fired every time itcurrentTime
is updated by the browser.I have taken your example, I have made the button go to 15 seconds from the end and then I have put an event listener so that it stops 5 seconds later, to see if it works for you.