session.speaker is audio output: play a URL, speak text, or stream live PCM.
Playback follows the phone’s media route, including connected Bluetooth audio
glasses. For audio input (mic frames, voice activity), use
session.mic instead.
src/background/index.ts
Methods
Playing a URL
play streams an audio URL through the phone’s playback service and resolves
when it finishes.
PlayAudioOptions:
Text-to-speech
speak sends the text to the phone, which by default streams cloud TTS when
connected and falls back to its on-device voice when the cloud is unavailable
(see Cloud or on-device). Cloud speech starts playing
while it is still being synthesized, the same way on Android and iOS. It resolves when playback completes. If the connection drops in the
middle of a sentence, the part that already played stays played and speak
rejects with TTS_UPSTREAM_ERROR. Offline playback may segment longer text
internally to reduce latency; callers use the same speak() API and receive
the same interruption behavior in either mode.
By default, the Mentra App converts display-oriented markup and symbols into
natural spoken words before either TTS path runs. For example, 68°F (20°C)
becomes “68 degrees Fahrenheit or 20 degrees Celsius.” Set
enableSanitization: false when the TTS engine must receive the exact input,
such as code, equations, or character-by-character content.
SpeakOptions:
SpeakResult has one field, completed: true if playback finished, false
if it was interrupted.
Queued by default
Eachspeak() waits for your miniapp’s earlier speech to finish, so calling it
three times in a row says all three sentences in order:
stopOtherAudio: true: it cuts off your miniapp’s speech in progress and
queued, stops other audio, and speaks at once. The cut-off calls resolve with
{completed: false}. speaker.stop() also clears the queue.
Cloud or on-device
processing takes the same values as for
transcription:
processing replaces {forceLocal: true}, which still works as processing: "localOnly" until SDK 4.0. When both
are set, processing wins. Mentra App versions before 3.4.0 don’t know processing: there, "preferLocal" behaves
like "preferCloud", and "localOnly" keeps working.Live PCM streaming
UsecreateStream() when audio arrives incrementally and cannot be exposed as
a complete file or URL—for example, live meeting audio received over a
WebSocket. The stream accepts signed 16-bit little-endian mono PCM on Android
and iOS.
src/background/index.ts
SpeakerStreamOptions:
Start delay
Playback starts oncejitterMs of audio is queued, or when you call
close(), so a sound shorter than jitterMs still plays. The same value then
protects against gaps when chunks arrive unevenly. The default 500 suits
one-way audio such as music or a recording. For conversational audio or a
short cue that should start right away, pass a small value:
jitterMs the same way.
Prewarming a stream
Opening an audio stream takes time. When you know a sound is coming, for example a “listening” chime after a wake word, open the stream early withprewarm: true so the sound only has to write samples:
idle, it doesn’t interrupt other audio, and other sounds (including your own
speak(), play(), and stop()) leave it open. Its first write starts it,
applying stopOtherAudio at that moment. If the audio route changes before
the first write (Bluetooth or glasses connect or disconnect, a phone or Teams
call, an audio interruption), the phone closes the stream, because it would
otherwise play on the old route. The next write then fails with “not open”;
open a fresh stream on the current route, as above. Opening another stream
still closes the previous one, since a miniapp has one stream at a time.
The returned SpeakerStreamWriter has:
Errors
speak rejects with an object carrying a code field on cloud-side TTS
failure. Catch it and branch on code:
State
Five states, read synchronously viastate or isPlaying, or observed as
transitions via onStateChange:
A request moves
idle → loading → playing → stopped. On failure the
speaker fires error once with errorCode set, then settles to stopped so
isPlaying reads false.
SpeakerStateEvent:
onStateChange does not fire immediately with the current value. Read state separately if you want the starting
value. Audio input (mic frames and voice-activity detection) lives on
session.mic.
