Skip to content

Commit 807295f

Browse files
ZhaoChaoqunclaude
andcommitted
Fix Streaming Paraformer by recreating stream after inputFinished
After calling inputFinished(), the stream cannot accept new audio. Must create a new stream for the next recording session instead of just calling reset(). - Add recreateStream() method to destroy old stream and create new one - Call recreateStream() in stopRecordingStreaming() after getting result - Call recreateStream() in startRecording() to ensure clean state Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 0e8ce22 commit 807295f

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

Sources/RecordingManager.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ class RecordingManager {
152152
case .funasrNano:
153153
vad?.reset()
154154
case .streamingParaformer:
155-
onlineRecognizer?.reset()
155+
// 确保使用新的 stream(防止上次 inputFinished 后状态异常)
156+
onlineRecognizer?.recreateStream()
156157
}
157158

158159
// 创建音频引擎
@@ -416,8 +417,8 @@ class RecordingManager {
416417
// 获取最终结果
417418
let text = recognizer.getResult()
418419

419-
// 重置流状态,清空残留数据
420-
recognizer.reset()
420+
// 重新创建流(inputFinished 后必须创建新流才能继续使用)
421+
recognizer.recreateStream()
421422

422423
DispatchQueue.main.async {
423424
var finalText: String? = nil

Sources/SherpaOnnxOnlineRecognizer.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,19 @@ class SherpaOnnxOnlineRecognizer {
130130
SherpaOnnxOnlineStreamReset(recognizer, stream)
131131
}
132132

133+
/// 重新创建流(在 inputFinished 后必须调用此方法才能开始新的识别)
134+
func recreateStream() {
135+
// 销毁旧的 stream
136+
if let oldStream = stream {
137+
SherpaOnnxDestroyOnlineStream(oldStream)
138+
}
139+
// 创建新的 stream
140+
stream = SherpaOnnxCreateOnlineStream(recognizer)
141+
if stream == nil {
142+
print(">>> SherpaOnnxOnlineRecognizer: 重新创建流失败")
143+
}
144+
}
145+
133146
/// 通知输入结束
134147
func inputFinished() {
135148
guard let stream = stream else { return }

0 commit comments

Comments
 (0)