[Guide - Card Template] Free TTS (Edge + offline Piper) with Play/Pause/Stop, no add-on install

Sharing a small script that adds text-to-speech to card templates, no add-on, just a script tag pasted into the note type’s template. Works on desktop, AnkiMobile, and AnkiDroid (anywhere the reviewer runs a web view with network/JS).

GitHub: GitHub - krmanik/anki-tts: MS Edge TTS in Anki Card Template · GitHub

What it does

Two TTS engines, switchable from a Settings panel during review:

  • Microsoft Edge (online) - the original “Read Aloud” voices, wide language/voice selection.
  • Piper (offline, on-device) - neural TTS that runs entirely in the browser. Voice models download once (10-70MB depending on quality) and are cached, so after the first play it works with no internet connection at all.
  • A gear icon during review opens Settings to pick engine, locale, voice, and (for Piper) speaker.
  • Play button turns into Pause/Resume while audio plays, plus a Stop button and a progress bar.
  • A collapsible Log panel shows every step (voice list, download progress, phonemes, synthesis) and auto-expands on error — useful since the Anki reviewer has no real devtools.

Setup

Add this script tag near the top of your card template (Front or Back, via the note type editor):

<script type="module" src="https://cdn.jsdelivr.net/gh/krmanik/anki-tts@latest/src/anki_tts.js"></script>

Then add a play button/trigger and point it at whichever field you want read aloud:

<script>
    stopTts?.();               // stop any audio left playing from the previous card

    function playTts() {
        let text = '{{Front}}';   // change Front to your field name, e.g. {{Hanzi}}
        ttsPlay(text);             // uses whichever engine/voice is selected in Settings
    }
</script>

About the Edge engine and browsers

Microsoft’s TTS server (speech.platform.bing.com) checks the real browser User-Agent and only accepts connections from actual Microsoft Edge. Since browsers don’t let JavaScript override that header, this can’t be worked around client-side, in this project or anything else, which means the Edge engine normally won’t work in Chrome, Firefox, Safari, or Anki’s own review window (desktop, mobile, or AnkiDroid).

Two ways around it, both optional (Piper doesn’t need either):

  1. Local relay (npm run relay) - runs a small Node process on your own machine that connects to Microsoft’s server with spoofed headers from outside the browser sandbox, where custom headers are allowed. The Edge engine auto-detects it on 127.0.0.1:8811 while it’s running. Good if you review on desktop and don’t mind a terminal open.

  2. Cloudflare Worker relay - same trick, but hosted so it’s always on with nothing running locally. Free tier (100k requests/day, no card required) easily covers personal use. Useful for AnkiMobile/AnkiDroid where a local relay isn’t reachable. Setup/deploy steps: worker/README.md. Once deployed, paste the Worker URL and your relay token into the Edge engine’s Settings panel (gear icon).

If neither relay is running, Edge TTS just falls back to “real Edge browser only”, everything else, including Piper, is unaffected.

View example usage in Anki-xiehanzi deck

https://krmanik.github.io/Anki-xiehanzi/create

Screenshots

Feedback, issues, and PRs welcome on the repo.

I’m not sure I understand what this is about. Adding tts to cards is already supported by Anki: Text to Speech for individual fields

In my cards it looks like that:

Anki uses local TTS provided by OS the app is running on. This offers alternative online services that might provide better quality in certain cases. Similar to HyperTTS add-on, only with less services but with cross-platform support instead of desktop only (the description doesn’t mention AnkiWeb, but I don’t see why it shouldn’t work there just as well).

He has it written down:

Microsoft’s TTS server (speech.platform.bing.com) checks the real browser User-Agent and only accepts connections from actual Microsoft Edge. Since browsers don’t let JavaScript override that header, this can’t be worked around client-side, in this project or anything else, which means the Edge engine normally won’t work in Chrome, Firefox, Safari, or Anki’s own review window (desktop, mobile, or AnkiDroid).

On a computer, it depends on the TTS engines installed. So, the best thing is to get an audio file that doesn’t rely on TTS and use that. But sometimes TTS is enough for me, and for some other languages, TTS isn’t available at all.

On device tts is little bit robotic voices, the edge tts is natural voice. Also piper tts is somewhat better and clear.

My project work on AnkiMobile, AnkiDroid also. I have anki deck (Anki xiehanzi) which embed db of lots of example sentences. Bundling audio file for each sentence is not feasible. So I used edge tts and piper tts for natural audio sound. On device tts is little bit robotic sound.

This is on device tts. I used edge tts and piper tts.

Not for me. The Google voices sound natural and the words have the cotrect stress accents. :slightly_smiling_face: