When to Use clearAudioYou can send a clearAudio event to interrupt the audio that has been sent to Vobiz. This will clear all the buffered audio. Common scenarios include:
- Barge-in: User starts speaking while AI prompt is playing
- Context switch: User changes topic mid-conversation
- Error recovery: Cancel current playback and play error message
- Priority messages: Interrupt to play urgent information
Attributes
Request & Response
Request Format
Send this JSON message through the WebSocket to Vobiz to clear buffered audio:clearAudio event request
Response Format
Vobiz acknowledges the clearAudio event with aclearedAudio response:
clearedAudio acknowledgment
sequenceNumber field helps track the order of events in the stream.
Common use cases
1. Voice Assistant Barge-In
When building conversational AI, users often start speaking before the AI finishes its response. Detect user speech and send clearAudio to stop the AI from continuing its playback.Detect speech and interrupt
2. Dynamic Content Updates
If you’re playing a long audio message and receive updated information (e.g., real-time data), you can interrupt the current playback and play the updated content.Update with new information
3. Error Handling & Recovery
When an error occurs during a transaction or API call, interrupt the current flow and play an appropriate error message.Handle errors gracefully
Implementation Examples
Node.js Example
Complete clearAudio implementation
Python Example
clearAudio with asyncio
Ordering and edge cases
clearAudioonly flushes audio that hasn’t reached the caller yet. Anything already played out cannot be recalled. KeepingplayAudiochunks small (~20 ms) maximises how much a barge-in can cancel.- Pending checkpoints are voided. Every
checkpointwhose audio was still queued at the moment of the flush will not produce aplayedStream. Reset your per-utterance state (timers,botSpeakingflags) when you sendclearAudio, and don’t wait on those acks. - Gate new playback on
clearedAudio. If youplayAudioimmediately afterclearAudiowithout waiting for theclearedAudioack, the new audio can race the flush and be partially dropped. Wait forclearedAudio, then enqueue the replacement. - Debounce barge-in detection. A single noisy
mediaframe can trip a naive energy threshold. Require a short run of speech-level frames (or use a proper VAD) before firingclearAudio, or you will cut off the bot on background noise. clearedAudiocarriessequenceNumberandstreamId. UsestreamIdto confirm it matches the active stream;sequenceNumberreflects ordering within the stream.
Barge-in that gates replacement audio on clearedAudio
Best Practices
Wait for clearedAudio Acknowledgment
Before sending new playAudio events after clearAudio, it’s best practice to wait for theclearedAudio acknowledgment to ensure the buffer is fully cleared.