Carpela
Carpela

Reputation: 2195

HTML5 Audio -Limit playback/download to first x seconds

I would like to limit playback of an audio file (in certain circumstances) to the first ten seconds.

Is there a way of using a combination of onTimeUpdate, currentTime and pause commands to ensure that playback cannot be processed past a certain point.

I don't want to modify the file as in other circumstances I'll want the full file.

I think I want to do something like.

Reset to 0 each time it gets to 10.

<audio controls ontimeupdate="myFunction(this)">
  <source src="horse.ogg" type="audio/ogg">
  <source src="horse.mp3" type="audio/mpeg">

</audio>
<script>
  function myFunction(event) {
    // Trying to stop the player if it goes above 1 second
    if (event.currentTime > 10) {
      event.pause;
      event.currentTime = 0
    }
  }
</script>

It resets appropriately, but but I can't get it to shut up. It just goes on a loop, ignoring the event.pause command.

Any ideas what I'm doing wrong?

Upvotes: 0

Views: 1851

Answers (1)

adeneo
adeneo

Reputation: 318182

pause() is a function

function myFunction(event) {
    // Trying to stop the player if it goes above 1 second
    if (event.currentTime > 10) {
        event.pause();
        event.currentTime = 0
    }
}

Upvotes: 3

Related Questions