> ## Documentation Index
> Fetch the complete documentation index at: https://docs.opengame.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Let players listen to OG Radio in your game

> Add OG Radio to a browser game so players pick stations and songs from your own menu, with the SDK's ready-made picker or controls you build.

OG Radio is OpenGame's music: thousands of songs on stations from Pixel to Phonk. With the SDK's radio, `radio` from `@opengamehq/sdk/radio` or `OpenGame.radio` on the page global, players can pick a station or a song from your game's menu and keep listening while they play.

The OpenGame page plays the music, at the volume the player set for the radio. Your game sends play, pause and skip, and hears back what is playing. It doesn't handle audio, so you add nothing to your audio code. When the player leaves your game, the song carries on in the site's own player.

## Before you start

* A game that already starts the SDK. [Connect the SDK](https://docs.opengame.io/guides/identity.md) covers `og.init()`.
* A way to try it on OpenGame. The radio plays only on an OpenGame page, so test it in a preview (`opengame deploy`). Under `opengame dev`, and anywhere outside OpenGame, radio calls return `unavailable`.

## Drop in the picker

The quickest way is the ready-made picker. It lists All radios and then each station with a song count, a search field, the station's genres, a numbered A to Z song list, and play, previous, next, shuffle, repeat and volume.

> [tab] Without a bundler
>
> Load `opengame-global.js` from the SDK package before your game, as [Load the page global](https://docs.opengame.io/engines/services.md#load-the-page-global) shows. It defines `window.OpenGame`, and the radio is `OpenGame.radio`. OpenGame adds this file for you only to builds that use threads, so ship it yourself:
>
> ```js menu.js theme={"system"}
> const picker = OpenGame.radio.mountPicker(document.querySelector("#music"));
>
> // When the menu closes:
> picker.destroy();
> ```

> [tab] With a bundler
>
> ```bash theme={"system"}
> npm i @opengamehq/sdk
> ```
>
> ```js menu.js theme={"system"}
> import { og } from "@opengamehq/sdk";
> import { radio } from "@opengamehq/sdk/radio";
>
> await og.init();
> const picker = radio.mountPicker(document.querySelector("#music"));
> ```

Give the element a height, such as your menu's panel, and the picker fills it. On a narrow screen the stations stack above the songs.

While focus is inside the picker it answers these keys, and they stop there, so a letter typed into the search doesn't move your player:

| Key | Does |
| - | - |
| `/` | Search |
| Up and Down | Move through stations or songs |
| Enter | Play the selected song |
| Space | Play or pause |
| `N`, `P` | Next, previous song |
| `S`, `R` | Shuffle, repeat |
| `-`, `=` | Volume down, up |

Pass `{ shortcuts: false }` to turn the letter keys off. The arrows, Enter and Tab keep working. Pause your keyboard handling while the picker is open, since a listener on `window` in the capture phase still sees each key first.

## Match your game's look

Pass a theme, or set the same values as CSS custom properties on any element around the picker:

```js theme={"system"}
OpenGame.radio.mountPicker(menu, {
  theme: { accent: "#ff3b30", background: "#0b0c0e", radius: "4px", font: "'Chakra Petch', sans-serif" },
  title: "Music",
});
```

```css menu.css theme={"system"}
#music {
  --og-radio-accent: #ff3b30;
  --og-radio-surface: #1a1c20;
}
```

| Option | Custom property | Colours |
| - | - | - |
| `background` | `--og-radio-bg` | The panel |
| `surface` | `--og-radio-surface` | Fields, hovered rows, the selected station |
| `text` | `--og-radio-text` | Text |
| `muted` | `--og-radio-muted` | Counts and secondary text |
| `accent` | `--og-radio-accent` | The playing song, the play button, shuffle and repeat when on |
| `accentText` | `--og-radio-accent-text` | Text on the accent |
| `border` | `--og-radio-border` | Lines |
| `radius` | `--og-radio-radius` | Corners |
| `font` | `--og-radio-font` | The typeface |

`theme: "light"` starts from a light palette. For anything further, the picker names these parts for `::part()`: `panel`, `stations`, `station`, `search`, `genres`, `genre`, `list`, `song`, `controls`, `control`, `volume` and `status`.

```css theme={"system"}
[data-og-radio]::part(song) { text-transform: uppercase; letter-spacing: .04em; }
```

## Build your own controls

Everything the picker does is in the API, if you would rather draw the radio yourself.

```js radio-menu.js theme={"system"}
const { radio } = OpenGame;

// Stations A to Z, with song counts and genres.
const stations = await radio.stations();

// Songs A to Z by title. `station` is a station id or "all".
const page = await radio.tracks({ station: "pixel", genre: "8bit", q: "dawn" });
if (page.ok) {
  for (const song of page.value.items) addRow(song.title, song.stationName, song.id);
}

// From the player's click on a row:
row.onclick = () => radio.play({ track: row.dataset.id, genre: "8bit" });

// What is playing, now and on each change.
radio.onChange((state) => {
  nowPlaying.textContent = state.track ? `${state.track.title}, ${state.track.stationName}` : "";
  playButton.setAttribute("aria-label", state.playing ? "Pause" : "Play");
});
```

`tracks()` searches within the scope you give it: a search on Pixel's 8-bit genre finds only 8-bit songs, and with `station: "all"` it searches all stations. Searches ignore case and accents. Results are always A to Z by title, `200` songs a page at most; page with `offset`, and `total` counts every match.

| Call | Does |
| - | - |
| `play()` | Resumes the radio as it was. |
| `play({ track })` | Plays that song. Next then plays from that station. |
| `play({ track, genre })` | Plays that song. Next plays from that genre. |
| `play({ station, genre? })` | Tunes a station, or one of that station's genres, and starts it. |
| `pause()` | Pauses. |
| `next()`, `previous()` | Skips. Previous restarts the song if it is more than three seconds in. |
| `setShuffle(on)`, `setRepeat(on)` | Shuffle the station, or repeat the song. |
| `setVolume(level)` | Sets the radio's volume, 0 to 1, in steps of 0.05, up to the level the player set. |

## What the player controls

The radio belongs to the player, so a few things are up to them:

* **Sound needs a press.** `play`, `next`, `previous`, shuffle, repeat and turning the volume up must be called from the player's click or key press. Called from a timer, a load handler or a game event, they return `gesture_required`. A press counts for a few seconds, as the browser's own rule for starting sound does. Pausing and turning the volume down always work.
* **Their pause stands.** When the player pauses the radio in OpenGame's player, your game cannot start it again for a few seconds, and then only from a new press.
* **Muted means quiet.** While the player has your game's sound off on OpenGame, or another part of the page is using the speakers, calls that make sound return `muted`, and turning the game's sound off pauses radio your game started. Watch `og.audio.onHostMute` to know when to offer Play again.
* **The volume is theirs.** Your game can turn the radio down and back up to the level the player set, but not louder. When your game closes, a volume it set goes back to the player's, and it isn't remembered.
* **Their choices stay private.** Until your game has played the radio, `state.station`, `state.track`, `state.genre`, `state.volume`, `state.shuffle` and `state.repeat` are `null`, so your game doesn't learn what the player chose elsewhere or what another game left playing. `state.playing` is always there, since the player can hear it. A station your game tunes is not remembered either. Beyond the radio's own state, the SDK tells your game nothing about the player.
* **The site's player is always there.** Once the radio is playing, OpenGame shows the site's player on the page, so the player can always stop it, whatever your game draws.

## Show what is playing

Each song has a `title`, a `stationName` and an `attribution` string, such as "Cartridge Dawn, OG Pixel Radio, OpenGame", for a now-playing line if your game wants one. You don't need to credit the music. OG Radio songs have no cover art: `art` is `null`, and `palette` gives the station's three colours, dark to light, for a tile or a swatch. `duration` is `null` until the song plays, then `state.duration` has it. The picker shows what is playing once your game has played the radio, and its volume slider waits until then too.

## Limits and errors

A game can make 120 radio requests a minute; past that, calls return `busy` for a moment. A search is at most 64 characters. A request OpenGame does not answer within 5 seconds returns `timeout`.

| Error | Meaning |
| - | - |
| `unavailable` | Not on an OpenGame page, or this page does not offer the radio. Check `radio.available`. |
| `gesture_required` | Call it from the player's click or key press. |
| `muted` | The player has your game's sound off on OpenGame. |
| `not_found` | No station, genre or song by that id. Use the ids `stations()` and `tracks()` return. |
| `invalid_request` | An argument has the wrong type or is too long, a genre came without a station, or `play` named a genre that station does not have. |
| `busy` | Too many requests. Wait a moment. |
| `timeout` | OpenGame did not answer. Try again. |
| `invalid_response` | OpenGame answered with something the SDK could not read. Try again, and update the SDK if it keeps happening. |

Calls return `{ ok: false, error }` instead of throwing, as the rest of the SDK does. The [SDK reference](https://docs.opengame.io/reference/sdk.md) lists all the types.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.