Skip to main content

voice.close()

close() 方法会断开与实时语音服务的连接并清理资源。这对于正确结束语音会话和防止资源泄漏非常重要。

🌐 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.

使用示例
Direct link to 使用示例

🌐 Usage Example

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

参数
Direct link to 参数

🌐 Parameters

此方法不接受任何参数。

🌐 This method does not accept any parameters.

返回值
Direct link to 返回值

🌐 Return Value

此方法不返回值。

🌐 This method does not return a value.

注意
Direct link to 注意

🌐 Notes

  • 完成实时语音会话后,始终调用 close() 以释放资源
  • 在调用 close() 之后,如果你想开始一个新会话,需要再次调用 connect()
  • 该方法主要用于维护持久连接的实时语音提供商
  • 如果调用不支持实时连接的语音提供商,它将记录一个警告并且不执行任何操作
  • 未能关闭连接可能导致资源泄漏并可能引发语音服务提供商的计费问题