Skip to main content
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 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.
Load opengame-global.js from the SDK package before your game, as 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:
menu.js
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: 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:
menu.css
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.

Build your own controls

Everything the picker does is in the API, if you would rather draw the radio yourself.
radio-menu.js
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.

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. Calls return { ok: false, error } instead of throwing, as the rest of the SDK does. The SDK reference lists all the types.