Produce coherent output without racing the receive loop.
1. Chunk scheduling
Receive model events independently from audio playback. Queue only current-turn audio and preserve order. Text or tool events may arrive alongside audio and need separate handling.
2. Turn boundaries
Distinguish a completed model turn from temporary silence or a network gap. Reset per-turn state on an explicit completion signal or a documented termination condition.
3. Audio resource cleanup
Release capture and playback resources when the owner is closed. Handle audio focus loss and routing changes. A screen exit should not leave the microphone active.
Worked scenario
Bluetooth disconnects midway through a response. The playback owner handles the routing change and updates state without creating a second output stream.
Apply it
Build a fake event stream with delayed audio chunks and a completion event. Verify ordering and cleanup.
Check your understanding
Playback resources are released on stop, cancellation and connection failure. Explain the decision and show evidence from your implementation or design. If you cannot demonstrate it yet, revisit the relevant section before continuing.