Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

onratechange — handling playback-rate changes

The onratechange attribute specifies JavaScript that handles the ratechange event, fired after a media element's playbackRate or defaultPlaybackRate changes.

Version: WHATWG HTML Living Standard; index snapshot date recorded separately

Syntax

<audio onratechange="JavaScript statements"></audio>
<video onratechange="JavaScript statements"></video>

onratechange is the event-handler attribute for ratechange. It is available on all HTML elements, but naturally applies primarily to audio and video. Its value is a JavaScript function body, not an event name or a Boolean value.

The browser queues the event when playbackRate or defaultPlaybackRate changes. playbackRate represents the effective playback speed: 1 means normal speed and 1.5 means one and a half times normal speed. defaultPlaybackRate specifies the default speed; changing it does not itself change the effective playback speed.

Inside the attribute code, this refers to the element and event provides the event. The corresponding JavaScript property media.onratechange accepts a function or null; assigning a function replaces the previous handler in that slot. An alternative is media.addEventListener('ratechange', handler). CSP can block code in event-handler attributes.

The example changes the rate without loading a file or starting playback. Its result appears when the queued event is handled, not synchronously during the assignment.

Examples

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>onratechange demo</title>
<audio id="media" onratechange="document.getElementById('result').textContent = event.type + ': ' + this.playbackRate;"></audio>
<output id="result">Waiting</output>
<script>
  document.getElementById('media').playbackRate = 1.5;
</script>
</html>

Result

ratechange: 1.5

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources