Play an audio starting in 10 seconds

Asked

Viewed 28 times

1

I’m controlling some audios on a website and I’m needing some specific audios to start on second 10, so far it’s like this:

var minhaMusica = new Audio();
minhaMusica.src = 'musica.mp3";
minhaMusica.play();

It would take something like that (I know it doesn’t work, just to illustrate):

 var minhaMusica = new Audio();
 minhaMusica.src = 'musica.mp3";
 minhaMusica.play().currentTime(10);

There’s something about that?

2 answers

1


I got:

var minhaMusica = new Audio();
minhaMusica.src = 'musica.mp3";
minhaMusica.currentTime = 10;
minhaMusica.play();

0

In the documentation of <audio> (Htmlmediaelement) exists the currentTime,:

audio.currentTime = 10

For example:

audio = window.document.querySelector("audio");
if (!(audio instanceof HTMLMediaElement)) {
   alert('O audio não é um HTMLMediaElement');
}

window.document.querySelector("[skip]").addEventListener("click", function(e){
  audio.currentTime += e.target.getAttribute("skip");
});

window.document.querySelector("[play]").addEventListener("click", function(e){
  audio.play();
});
<button skip="10">Avançar para 10s</button>
<button play>Começar</button>

<hr>

<audio controls="true">
  <source src="https://p.scdn.co/mp3-preview/5f3bc9c84ccb48505f511ee6ef6cc690483d9f5d" type="audio/mpeg">
</audio>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.