# Playback rate

Playback speed state and actions for the player store

Controls speed of playback. `playbackRateAvailability` is `unsupported` when the media has no playback rate, such as an embed whose provider takes no speed command.

## Import

```tsx
import { playbackRateFeature } from '@videojs/react';
```

The `audioFeatures` and `videoFeatures` [feature bundles](https://videojs.org/docs/framework/react/guides/presets) include this feature.

## API Reference

### Configuration

Props the Player component accepts. They exist only while this feature is selected.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `defaultPlaybackRates` | `undefined \| null \| readonly number[] \| string` | `[0.5, 0.75, 1, 1.25, 1.5, 1.75, 2]` | Rates the player's controls offer, in the order they cycle through them, unless a control sets its own `rates`. HTML takes a space-separated list, such as `default-playback-rates="0.5 1 1.5 2"`. Missing or empty input restores the defaults. Rates outside the media's range are left out, so YouTube, which plays up to `2`, may offer fewer. |

### State

| Property | Type | Description |
| --- | --- | --- |
| `playbackRate` | `number` | Current playback rate. |
| `playbackRateAvailability` | `'available' \| 'unavailable' \| 'unsupported'` | Whether the media can change its playback rate. `unsupported` when the attached media has no `playbackRate` at all, such as an embed whose provider takes no speed command. |
| `playbackRates` | `readonly number[]` | The default rates that fit the media's range. Set them through the player, not through the store. They are listed even when the media cannot change its rate, so check `playbackRateAvailability` before offering them. |

### Actions

| Action | Type | Description |
| --- | --- | --- |
| `setPlaybackRate` | `(rate: number) => void` | Set the playback rate, clamped to the rates the media accepts. |

### Playback rates

The player offers `0.5` through `2` in steps of `0.25` unless you set your own list. The [playback rate button](https://videojs.org/docs/framework/react/reference/components/playback-rate-button), the [playback rate radio group](https://videojs.org/docs/framework/react/reference/components/playback-rate-radio-group), and the `speedUp` and `speedDown` [hotkeys](https://videojs.org/docs/framework/react/guides/keyboard-shortcuts) use this list unless you give one of them its own.

Pass `defaultPlaybackRates` to [`Player`](https://videojs.org/docs/framework/react/reference/components/player):

```tsx
<Player defaultPlaybackRates={[0.5, 1, 1.25, 1.5, 2]}>
  <Container>
    <Video src="/video.mp4" />
  </Container>
</Player>
```

Removing the prop, or passing `null`, restores the defaults.

The button cycles through the rates in the order you list them, and the menu shows them in that order. `speedUp` and `speedDown` move to the next faster or slower rate whatever the order, and do nothing at the fastest or slowest rate. The player ignores values that aren’t positive numbers, and repeated rates. If nothing usable is left, it falls back to the defaults.

### Rates for one control

To give one control a different list, set `rates` on it. The rest of the player keeps the default list. The control offers these rates as given, even ones outside the media’s range, which play at the nearest end of the range.

```tsx
<PlaybackRateButton rates={[1, 1.5, 2]} />
```

To make the speed hotkeys change speed in fixed steps instead, give `speedUp` and `speedDown` a `value`. A `value` of `0.1` moves from `1` to `1.1`, then `1.2`, and so on.

### Rates outside the list

The media owns the current rate, so it can be one the menu doesn’t offer, such as `1.1` set through the media API or a stepped hotkey. The menu then shows that rate as an extra item, slotted before the next faster rate. Skins set it in italics, and its accessible name is “Custom, 1.1×”. The item goes away once the rate is back on the list. Normal speed (`1`) is added the same way when your list leaves it out, but isn’t marked custom.

### Media range

Each media accepts rates within a range. Browsers play `0.0625` through `16`, YouTube `0.25` through `2`, and Vimeo up to `2`. The player leaves out any `defaultPlaybackRates` outside that range and keeps the rest in your order. If none are left, it offers the defaults within the range and warns in development. `setPlaybackRate` clamps a rate outside the range to the nearest end, and warns in development. That includes `0`, which would stop the picture while the player still reports playback; call `pause()` instead.

`playbackRates` lists rates even for a media that can’t change its rate, so check `playbackRateAvailability` before offering them.

YouTube rounds rates down to a step of `0.05`, so asking for `1.13` plays at `1.1`.

### Selector

Pass `selectPlaybackRate` to [`usePlayer`](https://videojs.org/docs/framework/react/reference/api/use-player) to subscribe to playback rate state. Returns `undefined` if the playback rate feature is not configured.

Check `playbackRateAvailability` before showing a playback rate control.

**RateDisplay.tsx**

```tsx
import { selectPlaybackRate, usePlayer } from '@videojs/react';

function RateDisplay() {
  const rate = usePlayer(selectPlaybackRate);
  if (!rate || rate.playbackRateAvailability !== 'available') return null;

  return <span>{rate.playbackRate}x</span>;
}
```

---

React documentation: https://videojs.org/docs/framework/react/llms.txt
All documentation: https://videojs.org/llms.txt
