Mentra Miniapp SDK betaThe SDK is in beta, so its APIs may change before general availability.To put a miniapp in front of users, host it at an HTTPS URL and share the link, or get it listed in the Mentra Miniapp Store. See Publish your miniapp.Developing on Mentra Live? We recommend using the Mentra Bluetooth SDK.The developer tools are available in the Mentra App under Settings → Miniapp Developer Settings.Share feedback with an in-app bug report, on Discord, or by email at [email protected].
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

Each speak() waits for your miniapp’s earlier speech to finish, so calling it three times in a row says all three sentences in order:
To interrupt instead, as for a new answer that replaces the one being said, pass 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

Use createStream() 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.
Live PCM requires Mentra App 2.13.0 or newer. Set minHostVersion: "2.13.0" in a miniapp that depends on createStream() so older hosts reject installation instead of failing during a call.
src/background/index.ts
SpeakerStreamOptions:

Start delay

Playback starts once jitterMs 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:
Android and iOS treat 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 with prewarm: true so the sound only has to write samples:
Until its first write, a prewarmed stream is silent: the speaker state stays 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:
Always await writes and call either close() or abort(). The host also aborts the stream when the miniapp disconnects, but explicit teardown gives the best call-end latency. Compressed MP3, AAC, and Opus frames are not accepted; decode them to PCM before writing.

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 via state 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.