> Discover all available pages from the documentation index: https://mastra.zisheng.pro/llms.txt # voice.off() `off()` 方法用于移除之前通过 `on()` 方法注册的事件监听器。对于具备实时 Voice 功能的长期运行应用,此方法特别有助于清理资源和防止内存泄漏。 ## 使用示例 ```typescript import { OpenAIRealtimeVoice } from '@mastra/voice-openai-realtime' import chalk from 'chalk' // 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() // Define the callback function const writingCallback = ({ text, role }) => { if (role === 'user') { process.stdout.write(chalk.green(text)) } else { process.stdout.write(chalk.blue(text)) } } // Register event listener voice.on('writing', writingCallback) // Later, when you want to remove the listener voice.off('writing', writingCallback) ``` ## 参数 **event** (`string`): 要停止监听的事件名称(例如 'speaking'、'writing'、'error') **callback** (`function`): 传给 on() 的同一个回调函数 ## 返回值 此方法不返回值。 ## 注意事项 - 传给 `off()` 的 callback 必须与传给 `on()` 的函数引用相同 - 如果找不到 callback,此方法不会产生任何效果 - 此方法主要用于支持事件式通信的实时 Voice Provider - 如果在不支持事件的 Voice Provider 上调用此方法,它会记录警告且不执行任何操作 - 移除事件监听器对于防止长期运行的应用发生内存泄漏非常重要