Google Cast · Opus · Media Source Extensions
A working demo – gapless YTM albums on Cast, with a change to the receiver only
YouTube Music on Cast plays albums with a gap at every track change. On the devices I could test, a few hundred lines of JavaScript in the receiver page fix that, with no new devices and no server changes. I'd like to find out how far that holds on other Cast devices.
Source on GitHubMIT licence · no code from Google · public-domain album
Concept albums, live albums, DJ mixes and classical movements are split into tracks that are meant to flow into each other. On Cast speakers, YouTube Music puts a gap at every split. This page plays one continuous recording, cut into 14 tracks, in three ways. So far the last one, in its lossless form, has been seamless on a 2015 Chromecast Audio, also in a speaker group, and on a 2023 Google TV box, but not yet on every device (see which devices). The only thing that changes is the JavaScript receiver page (the small web app the Chromecast loads), the one part YouTube Music would need to change. Nothing changes in how YouTube encodes or serves its files, or in the phone apps and devices. You can try it on your own Cast speakers right from this page, and a short report, good or bad, helps show where it works.
01 Listen and report back
Play it right here, or press Cast to send it to your own Chromecast or speaker group. Pick a mode and play. You can switch modes while it plays, and it carries on from the same spot. The quickest test is the jump to join 10 → 11, which sits inside a rising crescendo.
Player log
Recording: Beethoven, Symphony No. 5, IV. Allegro, by the Skidmore College Orchestra (public domain, via Musopen). The whole movement, cut into tracks from 9 s to 3:22 long at arbitrary points mid-sound, and each one is encoded separately as Opus in WebM, just as music services stream them.
Tried it on your own Cast device? Report your result
So far this has been tested on only a handful of devices, so every result helps, including ones where it doesn't work. Open a pre-filled GitHub issue that you can review before posting. No GitHub account? Send it straight from this page instead (you see it first, no sign-in), or copy it as text and paste it wherever you found this page. It includes your receiver's firmware and browser version, but never your device's name.
Even a rough guess helps, like "Samsung TV" or "small Google speaker". Or just press the button again to send it without one.
| What you heard at the track changes | Gap | Click | Seamless | Not tried |
|---|---|---|---|---|
| Per-track, like YouTube Music | ||||
| Gapless queue | ||||
| Sample-accurate: lossless | ||||
| Sample-accurate: Opus bridge (experimental) |
Connect with Cast first, so the receiver's details are included.
You see everything before it is shared. The GitHub issue opens pre-filled, so you can read and change it before you submit. Sending without an account first shows you the full report, and you can cancel to change it. Copy puts the report on your clipboard, and you see it all when you paste it.
02 What you're hearing
The three modes differ only in what happens in the last few milliseconds of each track.
Per-track
What the YouTube Music Cast receiver does today. The track ends, the player tears down its stream, loads the next file and starts again. That's tens of milliseconds of silence in a desktop browser, and more on a Cast device.
Gapless queue
Tracks go back to back into one buffer, so there's no reload. But nothing removes the encoder's silent lead-in and padding, so a few milliseconds go missing or overlap. Some Cast devices also click where the buffer is spliced.
Sample-accurate
Each track is decoded, its lead-in and padding are cut off by sample count, and its first sample is placed right after the previous track's last one. The receiver hears one continuous waveform, exactly as it was recorded. The experimental Opus bridge option does the same trimming, but decodes and re-encodes only the ~0.2 s around each join.
How the audio gets to the speaker
YouTube Music on Cast today
Basic MSE gapless queue
Sample-accurate (this demo)
The first column reflects what we observed from YouTube Music's Cast receiver, which loads music track by track. Its player engine already has a gapless queue mode like the second column, but uses it only for Shorts. In the third column, the lossless way is the one tested in speaker groups so far, and the Opus bridge is for devices too slow to decode everything.
03 Why the last few milliseconds are hard
Every separately encoded Opus track carries a little extra audio that isn't part of the music:
- The file says exactly how much to drop: the pre-skip sits in the header, and the padding is a field on the last block. Desktop browsers honour some of this for a single file, but not across a queue.
- MSE positions tracks with millisecond timestamps. At 48 kHz that can't line up two tracks to the sample.
- Cast devices decode through their own media pipeline. On the Chromecast Audio and the onn 4K it clicks wherever a compressed stream is spliced, even with perfect timestamps.
04 A possible fix: swap one SourceBuffer
This is the change that worked on the devices tested so far. The player above it stays as it is. It keeps appending Opus into what it thinks is an ordinary SourceBuffer, keeps its queue and buffering, and seeks by clearing the buffer and appending again from the new point. Only the layer that hands audio to the device changes.
- Store the appended Opus, still compressed.
- Just ahead of the playhead, decode it with the browser's own decoder.
- Cut the pre-skip and padding by sample count.
- Butt each track against the previous track's last sample.
- Hand the result to MSE as lossless FLAC, a second at a time.
<script src="flac-mp4.js"></script>
<script src="sample-accurate-sb.js"></script>
<script>
SampleAccurateSB.enabled = true; // before the player calls addSourceBuffer()
</script>
- No new hardware on the devices tested so far, including a 2015 Chromecast Audio.
- No server changes: the same per-track Opus files.
- Speaker groups stayed in sync in testing, including adding a speaker mid-album (lossless way, Chromecast Audio).
- It can be switched on selectively, only for albums meant to play gaplessly. Every other album and single track keeps today's per-track playback exactly as it is.
05 Measured
Desktop Chrome, each join played from 4 s before the cut and compared with the original recording. For the per-track mode the table shows the inserted silence. For the queue modes it shows how far the audio moved at the cut, where 0 means seamless.
| Join | Per-track | Gapless queue | Sample-accurate |
|---|---|---|---|
| 1 → 2 | 29 ms silence | 3.5 ms lost | 0 samples |
| 4 → 5 | 18 ms silence | 2.7 ms | 0 samples |
| 9 → 10 | 19 ms silence | 0.15 ms | 0 samples |
| 10 → 11 (crescendo) | 79 ms silence | 3.9 ms | 0 samples |
| 13 → 14 | 19 ms silence | 0.17 ms | 0 samples |
On a Chromecast Audio (2015, Chrome 90 inside), the gapless queue clicks audibly at every join. The sample-accurate mode (lossless) is seamless on a single speaker and in a speaker group. A current Google TV box (onn 4K, 2023, latest update) behaves the same: gaps per track, a glitch at every join in the gapless queue, and seamless with the sample-accurate mode. On both devices the experimental Opus bridge option was also heard as seamless; it hasn't been measured here yet.
06 Why Spotify or Apple Music can't just copy this
This trick needs one thing: the receiver page must be able to decode the audio itself. That's what lets it cut the encoder's lead-in and padding to the sample.
YouTube Music
Streams ordinary, unencrypted Opus to the Cast receiver. The receiver page can decode it with the browser's own decoder, so the whole fix could fit in the receiver's JavaScript, as this page shows on the devices tested so far.
Spotify
Its Cast receiver plays every format, lossless included, through Widevine DRM. Decryption and decoding happen inside the device's protected media pipeline, and the decoded audio never reaches JavaScript, so it can't be trimmed there. It also starts a new media stream for each track.
Apple Music
Its Cast receiver streams encrypted AAC through Google's built-in Cast player, with Widevine DRM too. Again the page never sees decoded audio, and each track change goes through the player's queue.
That doesn't make gapless impossible for them, just harder. They could append tracks back to back into one encrypted buffer and let the device's decoder do the trimming, using the gapless information AAC files already carry. Only they can make that change, and nobody outside can test whether Cast devices splice encrypted audio cleanly. As far as I can tell from the outside, YouTube Music is in the unusual position where the fix could be entirely in its own receiver's JavaScript. (Based on the public Cast receiver pages of each service, as observed in November 2026.)
07 FAQ
About the idea
Hasn't anyone done this already?
Not this way, as far as I could find. Chrome's media team published the gapless recipe for desktop browsers back in 2015 (web.dev): read each file's encoder delay, then trim it with appendWindow and timestampOffset. It covers MP3 and AAC, and it is what the "Gapless queue" mode on this page does. With Opus on a Chromecast it still leaves a few milliseconds per join. Vimeo trims with MP4 edit lists (Vimeo engineering).
Cast apps that are gapless today join the tracks before they reach the speaker. Music Assistant's flow mode streams the whole queue from a server as one stream, which can cost metadata and device buttons. Hi-Fi Cast builds the stream on the phone, which must stay on the network. BubbleUPnP sends one URL per track and says it can't do gapless. Plex users have asked for it since 2018.
What's new here: everything happens inside the receiver page, with the service's existing per-track files, to the sample, with no server or phone in the loop, and in speaker groups too.
Why not just serve the album as one long file?
That breaks skipping, per-track metadata, play counts and caching, and it needs changes on the server side. This needs none of that: the files stay exactly as they are.
Why not fix it in the Chromecast firmware or in Chrome?
Older devices like the Chromecast Audio don't get firmware updates any more. The receiver page, on the other hand, is loaded fresh every time you cast, so a fix there could reach every device at once.
Why isn't the plain "gapless queue" good enough?
Its joins land a few milliseconds off. On the Chromecast Audio and the onn 4K that is audible as a click or a tiny hole, but it depends on the device: one tester reports it sounding flawless on a Google Home Mini. That may be one reason the obvious approach hasn't been shipped for music. See the measurements.
Impact on YouTube Music
Does this change anything for normal playback?
It wouldn't need to. It could be switched on only for albums meant to play without a break. Everything else would keep playing track by track, exactly as today.
Does YouTube have to change how it encodes or serves its files?
Not for this approach. The demo uses the same kind of per-track Opus files, unchanged, and only the receiver page's JavaScript changes.
Does it lower the audio quality?
Not in the lossless way: the Opus audio is decoded once, as it always is, and handed to the speaker losslessly as FLAC, with nothing re-encoded. The experimental Opus bridge option re-encodes only the ~0.2 s around each join, at a high bitrate (192 kbps).
Isn't it too heavy for old devices?
The lossless way runs well on a 2015 Chromecast Audio and keeps only about 5 seconds of decoded audio buffered ahead of the playhead. It is too heavy for a first-generation Google Home Mini, which is what the experimental Opus bridge option is for.
Do seek, skip, pause and the Google Home controls still work?
Yes. When you cast this demo, Google Home (or any other Cast remote) shows the album as a normal media session and can pause, seek, and skip to the next or previous track. Even though the gapless modes play one continuous stream, Home shows the current track's title, its length and the position inside it, and the title changes at the exact moment the music crosses into the next track. Seeks and skips continue the same stream from the new point instead of reloading it. Tested on a Chromecast Audio and a Google TV box.
What about speaker groups, or adding a speaker while it plays?
Both tested with Chromecast Audios in the lossless way, and both stayed gapless. The Opus bridge option hasn't been tested in a group yet.
In a speaker group, the receiver page runs on only one device, the group leader. The leader does all the work: it fetches the tracks, trims and joins them, and plays the result through its media element. The Cast firmware then sends that audio on to the other speakers and keeps them in sync. So only one device has to do the heavy lifting, and the other speakers just play what they receive. This is also why the demo hands its audio to the speaker through a normal media element instead of Web Audio: in our tests, groups only passed on the media element's audio. Adding a speaker mid-album just adds one more listener to the leader's stream.
Could it do crossfade too?
Probably, since the receiver has the decoded audio in hand. That's out of scope for this demo.
Other services
Why can't Spotify or Apple Music do the same?
Their audio is protected by DRM, so their receiver pages never see the decoded sound. See section 06.
The demo itself
Is this YouTube's code?
No. It is our own code, under the MIT licence, plus the open-source Opus codec (libopus, BSD licence, compiled to JavaScript by opusscript, MIT) for devices that can't decode Opus themselves. The page only describes YouTube Music's observed behaviour, and the music is a public-domain recording.
Which browsers can cast this page?
Chrome on a computer or on Android, and Edge on a computer. Google's Cast sender library for web pages only runs in those. On iPhone and iPad, no browser can start a cast from a web page.
Will it work on my Nest speaker, Google TV or TV with Chromecast built in?
Confirmed so far on a Chromecast Audio (2015) and a Google TV box (onn 4K, 2023). Nest speakers and TVs with Chromecast built in may behave the same way, but haven't been tested yet. On a first-generation Google Home Mini the sample-accurate mode can't decode fast enough, so it doesn't work there for now. Its experimental Opus bridge option is meant for devices like that and is still being tested there. Please try it and report your result.
What does a device report send?
Only what you see before you submit it: the model you type in, your verdicts, and technical details such as the receiver's browser version and codec support. Device names like "Living room" are never read or sent.
What is the music?
Beethoven's Symphony No. 5, movement IV, played by the Skidmore College Orchestra (public domain, via Musopen). It is cut into 14 tracks of deliberately uneven lengths, so the joins fall at awkward places.
About me
Who made this? Is it affiliated with Google or YouTube?
I'm a mid-level Java/Kotlin backend developer with no formal IT education: I majored in plant physiology and plant molecular biology. This is an independent project, not affiliated with Google or YouTube.
How long did this take?
Three evenings, while juggling family life with two small kids, armed with Claude Code and Codex. A production version would need much more testing across devices, but it suggests the core change is not a huge one.
Did you write this with AI?
Yes, heavily. Most of the code and much of the text were written by AI coding assistants (Claude Code and Codex) from my prompts. My part was the goal and the ideas to try, such as re-encoding only the moments around each join for weaker devices. I also tested every version on real speakers and decided what was good enough. Without these tools I couldn't have built this: I'm a backend developer, not an audio or media engineer. As far as I could find, nobody had shown this working before. That's why the measurements and device reports are on this page: please judge it by whether it plays gaplessly on your speakers, not by who typed the code.
English isn't my first language either, so I let the AI help with the wording too: getting the point across mattered more to me than polishing my own sentences. If the text reads a bit like an AI wrote it, sorry about that. I hope you can still look past the style to the substance.
Why did you make this?
Gapless playback on Chromecast is often dismissed as impossible, or blamed on the hardware. It turned out to work on the devices I have, and I'd like to find out how widely that holds. A public example that anyone can try on their own speakers is the quickest way to collect those results.
Why do you care so much about gapless?
I'm a big fan of Gorillaz, the Beatles, the Avalanches and Daft Punk, and our home has three Chromecast Audios that are playing most of the day. Demon Days, the Abbey Road medley, Since I Left You and Homework are built to flow from one track into the next, and every gap between them breaks my heart a little bit.