Unsupported playbackRate exception Sample

Available in Chrome 63+ | View on GitHub | Browse Samples

Background

When media elements's playbackRate is set to a value not supported by Chrome (eg. negative value), a "NotSupportedError" DOMException is thrown.

Credits: Audio file is © copyright Blender Foundation | www.blender.org .

Live Output


JavaScript Snippet

const audio = document.querySelector('audio');

function onButtonClick(event) {
  const text = event.target.textContent;
  const playbackRate = Number(/playbackRate = (-?\d+([/.]\d+)?)/.exec(text)[1]);

  try {
    audio.playbackRate = playbackRate;
  } catch(error) {
    log('> Error: ' + error.message);
  }
}

audio.addEventListener('ratechange', function(event) {
  log('> Playback rate changed to ' + event.target.playbackRate);
});