docs/src/content/en/reference/voice/voice.close.mdx
The close() method disconnects from a real-time voice service and cleans up resources. This is important for properly ending voice sessions and preventing resource leaks.
import { OpenAIRealtimeVoice } from '@mastra/voice-openai-realtime'
import { getMicrophoneStream } from '@mastra/node-audio'
// Initialize a real-time voice provider
const voice = new OpenAIRealtimeVoice({
realtimeConfig: {
model: 'gpt-5.1-realtime',
apiKey: process.env.OPENAI_API_KEY,
},
})
// Connect to the real-time service
await voice.connect()
// Start a conversation
voice.speak("Hello, I'm your AI assistant!")
// Stream audio from a microphone
const microphoneStream = getMicrophoneStream()
voice.send(microphoneStream)
// When the conversation is complete
setTimeout(() => {
// Close the connection and clean up resources
voice.close()
console.log('Voice session ended')
}, 60000) // End after 1 minute
This method doesn't accept any parameters.
This method doesn't return a value.
close() when you're done with a real-time voice session to free up resourcesclose(), you'll need to call connect() again if you want to start a new session