Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

213
Visualizações
Update position of seeker (i.e html input type range) while an audio is playing

I want the range slider automatically change while audio play.

<input type="range" name="seek" id="seeker" min="0" max="100" value="0" > 

let music = new Audio('https://www.computerhope.com/jargon/m/example.mp3');
music.play();
<input type="range" name="seek" id="seeker" min="0" max="100" value="0" > 

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Since new Audio() actually creates a new <audio> element (it is just not injected into your DOM), you can still listen to DOM events. For your use-case, you will want to listen to the timeupdate event, and get the progress of the play through by dividing the total duration of the audio clip with currentTime:

music.addEventListener('timeupdate', () => {
  const percent = music.currentTime / music.duration * 100;
  seeker.value = percent;
});

However, if you want the users to use the <input> element to seek through the audio clip (I assumed that since you named your element #seeker), then you will also need to listen to the input event from the element. You can use simple math to calculate what currentTime value you have to set on the audio element.

Here is a proof-of-concept example. I have ensured that play() is only triggered by a button click, since most browsers do not autoplay audio files today.

const music = new Audio('https://www.computerhope.com/jargon/m/example.mp3');
const seeker = document.querySelector('#seeker');

music.addEventListener('timeupdate', () => {
  const percent = music.currentTime / music.duration * 100;
  seeker.value = percent;
});

// Allow seeking
seeker.addEventListener('input', (e) => {
  music.currentTime = e.target.value / 100 * music.duration;
});

document.querySelector('#play-button').addEventListener('click', () => {
  music.play();
});
<input type="range" name="seek" id="seeker" min="0" max="100" value="0">
<button type="button" id="play-button">Play</button>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda