<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Building UMDIA]]></title><description><![CDATA[Building UMDIA]]></description><link>https://umdia.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Sat, 05 Dec 2026 21:42:04 GMT</lastBuildDate><atom:link href="https://umdia.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Pronunciation for a whole dictionary with zero audio files: the Web Speech API on iOS]]></title><description><![CDATA[UMDIA is an iPhone app for learning European Portuguese, and every word needs a "listen" button. Recording a native speaker for every word and example…]]></description><link>https://umdia.hashnode.dev/pronunciation-for-a-whole-dictionary-with-zero-audio-files-the-web-speech-api-on-ios</link><guid isPermaLink="true">https://umdia.hashnode.dev/pronunciation-for-a-whole-dictionary-with-zero-audio-files-the-web-speech-api-on-ios</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[iOS]]></category><category><![CDATA[Web Development]]></category><dc:creator><![CDATA[João from UMDIA]]></dc:creator><pubDate>Fri, 04 Dec 2026 20:17:48 GMT</pubDate><content:encoded><![CDATA[<p>UMDIA is an iPhone app for learning European Portuguese, and every word needs a "listen" button. Recording a native speaker for every word and example sentence wasn't realistic for a one-person app, and paid text-to-speech APIs cost money per request and need a network. So the app uses the voices that already ship with the phone. Here is what I learned getting that to sound right.</p>
<h2>Pick the voice explicitly</h2>
<p>On iOS the app runs in a WKWebView, which exposes <code>speechSynthesis</code>. If you only set <code>utterance.lang = 'pt-PT'</code>, WebKit is free to fall back to whatever Portuguese voice it likes, and for a European Portuguese app a Brazilian accent is exactly the wrong result. So the app looks for an exact language match first, and prefers the best quality installed:</p>
<pre><code class="language-js">const QUALITY = /premium|enhanced|melhorad|aprimorad/i

export function pickVoice(voices, lang = 'pt-PT') {
  const target = lang.toLowerCase()
  const exact = voices.filter(v =&gt; (v.lang || '').replace('_', '-').toLowerCase() === target)
  const pool = exact.length ? exact : voices.filter(v =&gt; (v.lang || '').toLowerCase().startsWith('pt'))
  if (!pool.length) return null
  return pool.find(v =&gt; QUALITY.test(v.name)) || pool[0]
}
</code></pre>
<p>The regex covers voice names in English and in Portuguese, because the names follow the device language.</p>
<h2>The voice list arrives late</h2>
<p>On iOS and in Chrome, <code>getVoices()</code> often returns an empty array the first time. The list shows up later with a <code>voiceschanged</code> event. Waiting for that event forever is a bad idea too, because some setups never fire it, so the app waits for whichever comes first, the event or 600 ms:</p>
<pre><code class="language-js">function voicesReady() {
  const synth = window.speechSynthesis
  const now = synth.getVoices()
  if (now.length) return Promise.resolve(now)
  return new Promise(resolve =&gt; {
    const done = () =&gt; resolve(synth.getVoices())
    synth.addEventListener?.('voiceschanged', done, { once: true })
    setTimeout(done, 600)
  })
}
</code></pre>
<h2>Only the latest request may speak</h2>
<p>That wait created a race. In a quiz, a player taps through questions quickly. Request A is waiting for voices, the player moves on, request B cancels the audio and speaks, and then A finally resolves and says the previous word over the new question. The fix is a counter: every request takes a number, and a request only speaks if its number is still the latest when it's ready.</p>
<pre><code class="language-js">let current = 0
export async function speak(text) {
  const id = ++current
  const voices = await voicesReady()
  if (id !== current) return // a newer request (or a stop) came in
  // ...build the utterance and speak
}
export function stop() { current++; speechSynthesis.cancel() }
</code></pre>
<p><code>stop()</code> bumps the counter too, so leaving a lesson halfway cancels anything still waiting.</p>
<h2>Acronyms</h2>
<p>Portuguese says some acronyms as words. The tax number, NIF, is "nif", but the iPhone voice spells it out. Lower case didn't fix it. The next update replaces it with "nife" before speaking, which the voice reads as one word; the final "e" in European Portuguese is nearly silent, so it sounds right.</p>
<h2>Android is different</h2>
<p>Android's WebView has no <code>speechSynthesis</code> at all, so for the Android build the same functions call the native engine through <code>@capacitor-community/text-to-speech</code>. The rest of the app doesn't know which path it's using.</p>
<h2>Was it worth it?</h2>
<p>For a vocabulary app, yes. There's nothing to record, nothing to download, it works offline, and adding a word to the dictionary means it can be heard straight away. The trade-off is that you depend on the voices installed on the device, so it pays to test on real devices with the voice you expect people to have.</p>
<p>UMDIA is free to start if you're learning European Portuguese: <a href="https://apps.apple.com/app/id6819083275?pt=129560377&amp;ct=hashnode&amp;mt=8">App Store</a> · <a href="https://umdia.co">umdia.co</a></p>
]]></content:encoded></item></channel></rss>