This commit is contained in:
zpc 2025-03-28 03:59:59 +08:00
parent f99f4e1b4a
commit 97f852d5e5
3 changed files with 70 additions and 13 deletions

View File

@ -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)

View File

@ -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 = `<span class="realtime-text" style="color: #0d6efd;">${text}</span> <small class="text-muted">(实时识别中...)</small>`;
log("显示实时语音识别结果");
}
// 显示最终语音识别结果
function showFinalTextResult(text) {
// 显示文本区域
const textDisplayArea = document.getElementById("text-display-area");
textDisplayArea.style.display = "block";
// 设置文本内容,并标记为最终结果
const displayedText = document.getElementById("displayed-text");
displayedText.innerHTML = `<span class="final-text">${text}</span>`;
// 同时将文本填入输入框,便于编辑
// document.getElementById("text-input").value = text;
log("显示最终语音识别结果");
}
</script>
}

View File

@ -86,6 +86,11 @@ namespace ShengShengBuXi.Services
/// </summary>
public List<string> RecognizedTexts { get; set; } = new List<string>();
/// <summary>
/// 临时语音识别文本
/// </summary>
public string VoiceTextStr { get; set; }
/// <summary>
/// 会话创建时间
/// </summary>
@ -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);
}