diff --git a/ShengShengBuXi/Hubs/AudioHub.cs b/ShengShengBuXi/Hubs/AudioHub.cs
index 14b147b..3124594 100644
--- a/ShengShengBuXi/Hubs/AudioHub.cs
+++ b/ShengShengBuXi/Hubs/AudioHub.cs
@@ -413,8 +413,13 @@ namespace ShengShengBuXi.Hubs
_logger.LogInformation($"结束音频流: {Context.ConnectionId}");
+
+
+
// 结束音频处理
await _audioProcessingService.EndAudioStreamAsync(Context.ConnectionId);
+ // 结束语音识别会话
+ await _speechToTextService.EndSessionAsync(Context.ConnectionId);
var _speechsession = _speechToTextService.GetSessionStatus(Context.ConnectionId);
if (_speechsession.HasSavedText && !_speechsession.IsSentToDisplay)
@@ -422,13 +427,11 @@ namespace ShengShengBuXi.Hubs
AddRecognizedTextToDisplay(_speechsession.FinalText, true, Context.ConnectionId, _speechsession);
_speechsession.IsSentToDisplay = true;
- _logger.LogInformation($"结束音频流: {Context.ConnectionId}");
+ _logger.LogInformation($"保存文件流: {JsonConvert.SerializeObject(_speechsession)}");
+ // 将结果发送给所有监听中的客户端
+ await _hubContext.Clients.Groups(new[] { "webadmin", "monitor" })
+ .SendAsync("ReceiveSpeechToEndTextResult", _speechsession.FinalText);
}
-
-
- // 结束语音识别会话
- await _speechToTextService.EndSessionAsync(Context.ConnectionId);
-
// 更新客户端状态
clientInfo.IsAudioStreaming = false;
_clients[Context.ConnectionId] = clientInfo;
@@ -746,20 +749,21 @@ namespace ShengShengBuXi.Hubs
// 只处理最终结果,且不是仅用于显示的中间结果
if (e.IsFinal && !e.IsForDisplayOnly)
{
- _logger.LogInformation($"接收到语音识别结果: {e.Text}");
+ _logger.LogInformation($"接收到语音识别最终结果: {e.Text}");
// 将结果添加到显示文本队列
//AddRecognizedTextToDisplay(e.Text, true, e.Id);
- // 将结果发送给所有监听中的客户端
+ // 将最终结果发送给所有监听中的客户端
await _hubContext.Clients.Groups(new[] { "webadmin", "monitor" })
- .SendAsync("ReceiveSpeechToTextResult", e);
+ .SendAsync("ReceiveSpeechToEndTextResult", e.Text);
}
else
{
- // 中间结果也发送给客户端,但标记为非最终结果
+ // 发送实时识别结果给客户端
+ _logger.LogInformation($"接收到语音识别实时结果: {e.Text}");
await _hubContext.Clients.Groups(new[] { "webadmin", "monitor" })
- .SendAsync("ReceiveSpeechToTextResult", e);
+ .SendAsync("ReceiveSpeechToTextResult", e.Text);
}
}
catch (Exception ex)
diff --git a/ShengShengBuXi/Pages/Monitor.cshtml b/ShengShengBuXi/Pages/Monitor.cshtml
index 8f555a6..dbabc4a 100644
--- a/ShengShengBuXi/Pages/Monitor.cshtml
+++ b/ShengShengBuXi/Pages/Monitor.cshtml
@@ -351,6 +351,20 @@
log("错误: " + message);
showMessage("错误: " + message, "danger");
});
+
+ // 接收实时语音识别结果
+ connection.on("ReceiveSpeechToTextResult", (result) => {
+ log(`接收到实时语音识别结果: ${result.substring(0, 30)}${result.length > 30 ? '...' : ''}`);
+ // 显示实时识别结果
+ showRealtimeTextResult(result);
+ });
+
+ // 接收最终语音识别结果
+ connection.on("ReceiveSpeechToEndTextResult", (result) => {
+ log(`接收到最终语音识别结果: ${result.substring(0, 30)}${result.length > 30 ? '...' : ''}`);
+ // 显示最终识别结果
+ showFinalTextResult(result);
+ });
// 显示模式更新消息
connection.on("DisplayTypeChanged", (displayType) => {
@@ -1313,5 +1327,34 @@
log("处理实时音频失败: " + e);
}
}
+
+ // 显示实时语音识别结果
+ function showRealtimeTextResult(text) {
+ // 显示文本区域
+ const textDisplayArea = document.getElementById("text-display-area");
+ textDisplayArea.style.display = "block";
+
+ // 设置文本内容,添加一个标记表明这是实时结果
+ const displayedText = document.getElementById("displayed-text");
+ displayedText.innerHTML = `${text} (实时识别中...)`;
+
+ log("显示实时语音识别结果");
+ }
+
+ // 显示最终语音识别结果
+ function showFinalTextResult(text) {
+ // 显示文本区域
+ const textDisplayArea = document.getElementById("text-display-area");
+ textDisplayArea.style.display = "block";
+
+ // 设置文本内容,并标记为最终结果
+ const displayedText = document.getElementById("displayed-text");
+ displayedText.innerHTML = `${text}`;
+
+ // 同时将文本填入输入框,便于编辑
+ // document.getElementById("text-input").value = text;
+
+ log("显示最终语音识别结果");
+ }
}
\ No newline at end of file
diff --git a/ShengShengBuXi/Services/SpeechToTextService.cs b/ShengShengBuXi/Services/SpeechToTextService.cs
index 2abbc86..47fea03 100644
--- a/ShengShengBuXi/Services/SpeechToTextService.cs
+++ b/ShengShengBuXi/Services/SpeechToTextService.cs
@@ -86,6 +86,11 @@ namespace ShengShengBuXi.Services
///
public List RecognizedTexts { get; set; } = new List();
+ ///
+ /// 临时语音识别文本
+ ///
+ public string VoiceTextStr { get; set; }
+
///
/// 会话创建时间
///
@@ -264,8 +269,13 @@ namespace ShengShengBuXi.Services
_logger.LogInformation($"结束语音识别会话: {sessionId}");
// 获取会话
- if (_sessions.TryGetValue(sessionId, out var session) && session.RecognizedTexts.Count > 0)
+ if (_sessions.TryGetValue(sessionId, out var session) && (session.RecognizedTexts.Count > 0 || session.VoiceTextStr?.Length > 0))
{
+ //
+ if (session.RecognizedTexts.Count == 0)
+ {
+ session.RecognizedTexts.Add(session.VoiceTextStr ?? "");
+ }
// 添加去重处理
var uniqueTexts = RemoveDuplicateTexts(session.RecognizedTexts);
string finalText = string.Join("\n", uniqueTexts);
@@ -881,7 +891,7 @@ namespace ShengShengBuXi.Services
CreatedAt = DateTime.Now,
IsForDisplayOnly = true // 标记为仅用于显示的中间结果
};
-
+ session.VoiceTextStr = recognizedText;
// 触发临时结果事件
OnResultReceived(tempResult);
}