基于ChatGPT、LiveKit和Flutter的音转文:远程转录获取问题
解决LiveKit中获取本地/远程转录流的问题
方案1:自定义Room子类暴露转录流
LiveKit的Room类里的_onTranscriptionEvent是私有方法,但可以通过自定义子类的方式,把内部的转录事件转成公开的Stream对外提供:
import 'package:livekit_client/livekit_client.dart'; class CustomRoom extends Room { final _transcriptionController = StreamController<EngineTranscriptionReceivedEvent>.broadcast(); // 对外公开的转录事件流 Stream<EngineTranscriptionReceivedEvent> get transcriptionStream => _transcriptionController.stream; @override void _onTranscriptionEvent(EngineTranscriptionReceivedEvent event) { super._onTranscriptionEvent(event); // 把内部事件转发到公开流 _transcriptionController.add(event); } @override void dispose() { _transcriptionController.close(); super.dispose(); } }
之后用CustomRoom替代原有的Room实例,就能通过transcriptionStream监听所有转录事件(包含本地和机器人的远程转录)。
方案2:临时修改LiveKit源码应急
如果自定义子类的方式受限,可以直接修改LiveKit仓库的源码:
- 在
room.dart的Room类中添加一个公开的Stream控制器:final transcriptionEvents = StreamController<EngineTranscriptionReceivedEvent>.broadcast(); - 然后在
_onTranscriptionEvent方法末尾,把事件转发到这个公开流:void _onTranscriptionEvent(EngineTranscriptionReceivedEvent event) { // 保留原有处理逻辑 ... // 新增:转发事件到公开流 transcriptionEvents.add(event); } - 记得在
Room的dispose方法里关闭这个Stream控制器,避免内存泄漏。
这样就能直接通过room.transcriptionEvents获取所有转录事件了。
区分本地/远程转录
拿到EngineTranscriptionReceivedEvent后,直接通过参与者对象判断:
- 当
room.localParticipant == event.participant时,是本地用户的转录内容 - 否则就是机器人的远程转录内容
内容的提问来源于stack exchange,提问作者geovane
相关产品推荐
相关产品推荐

