CXR-L(EN)
cxr-l-sdk · v1.0.1 · snapshot 2026-05-13· ↗ source

Audio

Android: Audio

Scenario

On an existing CXRLink (created from either the CustomView or CustomApp flow), start the glasses-side audio stream. The SDK delivers PCM chunks via IAudioStreamCbk.onAudioReceived. The Sample writes the data to a file, wraps it into a WAV (16 kHz, mono, 16-bit), and supports local playback and sharing.

Prerequisites

  • CXRLSampleApplication.sharedCxrLink has been assigned and connect has completed (see AudioUsageViewModel.init: if null then _ready=false).
  • The glasses-side scene is built: using the Sample as an example, before entering this page from the Sample entry, you must complete customViewOpen in the CustomView flow (customViewOpened is true), or complete openApp in the CustomApp flow (appOpened is true). You must not call startAudioStream when only the link is connected but the scene is not opened.
  • The Intent may carry CONSTANT.EXTRA_ENTRY_TYPE as customView or customApp. In the Sample it is only used for UI copy differences (does not affect the startAudioStream call itself).

Key APIs

MethodDescription
setCXRAudioCbk(IAudioStreamCbk)Register the audio stream callback (can be separate from setCXRLinkCbk).
startAudioStream(codecType: Int)The Sample uses codecType = 1. Returns Boolean indicating whether the request is initiated (final status follows logs/callbacks).
stopAudioStream()Stop capture; the Sample calls it in stopAudio and release.

Callbacks (IAudioStreamCbk)

CallbackDescription
onAudioReceived(data, offset, length)PCM segment. The Sample bounds-checks offset/length before writing to FileOutputStream.
onAudioStreamStateChanged(started: Boolean)Stream state changes.
onAudioError(code, info?)Stops and prompts on error.

PCM to WAV (Sample)

  • Fixed sample rate 16000, channels 1, bit depth 16 (see buildWavFromPcm).
  • On stop, duration is estimated by total bytes: actualPcmSize / (16000 * 2) seconds.

Constraints

  • Start streaming only when Bluetooth and CXR service are available; otherwise prompt the user to reconnect first (_ready / _status).
  • For frequent disk writes, flush by chunks (the Sample flushes for the first few packets and every 20 packets). On finish, call fd.sync() to best-effort persist data.
  • In release() on page destroy, call stopAudioStream() to avoid background resource usage.

Sample reference paths

  • activities/audio/AudioUsageViewModel.kt
  • activities/audio/AudioUsageActivity.kt
Marcin Miazga