user3792326
user3792326

Reputation:

Javascript Play Audio at Certain Point

I am making a webpage that contains an audio file implemented through the HTML audio tag. The audio is currently controlled all by the viewer, however, I would like to have a button (HTML or Javascript) that, when clicked, will start playing the audio from a certain time point. How can I achieve this? Thanks

Upvotes: 0

Views: 2468

Answers (2)

brybott
brybott

Reputation: 157

I took the following answer from examples in the documentation: https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Using_HTML5_audio_and_video

var mediaElement = document.getElementById('mediaElementID');
mediaElement.currentTime = 122; //Skips to 122 seconds into the song

Just make a button and call the above script when it is clicked and it will skip to the point in the song specified by the number of seconds you give it.

Upvotes: 5

Salvador Medina
Salvador Medina

Reputation: 6581

From the same page you can develop something like:

<html>
<audio id="demo" src="The Runaways - Cherry Bomb.mp3"></audio>
<div>
  <button onclick="document.getElementById('demo').play()">Play the Audio</button>
  <button onclick="document.getElementById('demo').pause()">Pause the Audio</button>
  <button onclick="document.getElementById('demo').volume+=0.1">Increase Volume</button>
  <button onclick="document.getElementById('demo').volume-=0.1">Decrease Volume</button>
  Jump to <input id="time" type="text" value="30"> (in seconds)</input>
  <button onclick="document.getElementById('demo').currentTime=document.getElementById('time').value">Jump</button>
</div>
</html>

Upvotes: 1

Related Questions