我一直致力于实现 Azure 通信服务 (ACS) 实时转录功能,并按照以下内容中概述的步骤进行操作:https://learn.microsoft.com/en-us/azure/communication-services/how- tos/呼叫自动化/实时转录教程?pivots=编程语言-csharp
这是我遵循的工作流程:
在前端,我使用 Call Composite 启动呼叫时捕获 serverCallId。
[HttpPost("start-tanscription")]
public async Task<IActionResult> StartTranscription([FromBody] StartRecordingRequest request)
{
try
{
var serverCallId = new ServerCallLocator(request.ServerCallId);
var websocketUri = callbackUriHost.Replace("https", "wss") + "ws";
_logger.LogInformation($"Callback url: {callbackUri}, websocket Url: {websocketUri}");
var callInvite = new GroupCallLocator(request.GroupCallId);
var connectOptions = new ConnectCallOptions(callInvite, callbackUri)
{
CallIntelligenceOptions = new CallIntelligenceOptions()
{
CognitiveServicesEndpoint = new Uri(_cognitiveServicesEndpoint)
},
TranscriptionOptions = new TranscriptionOptions(
new Uri(websocketUri),
"en-US",
false,
TranscriptionTransport.Websocket
),
};
var createCallResultResponse = _callAutomationClient.ConnectCall(connectOptions);
var callConnectionMedia = createCallResultResponse.CallConnection.GetCallMedia();
StartTranscriptionOptions startTrasnscriptionOption = new StartTranscriptionOptions()
{
Locale = "en-US",
OperationContext = "startMediaStreamingContext"
};
await callConnectionMedia.StartTranscriptionAsync(startTrasnscriptionOption);
_logger.LogInformation("Real-time transcription started...");
_callAutomationClient.GetEventProcessor().AttachOngoingEventProcessor<TranscriptionFailed>(
createCallResult.CallConnection.CallConnectionId, async (TranscriptionFailed) =>
{
_logger.LogInformation($"Received transcription event: {TranscriptionFailed.GetType()}, CorrelationId: {TranscriptionFailed.CorrelationId}, " +
$"SubCode: {TranscriptionFailed?.ResultInformation?.SubCode}, Message: {TranscriptionFailed?.ResultInformation?.Message}");
});
return Ok(new { CallConnectionId = createCallResult.CallConnection.CallConnectionId });
}
catch (Exception ex)
{
_logger.LogError(ex, "Error occurred while starting the recording.");
return BadRequest(new { error = ex.Message });
}
}
成功返回 202 和 CallConnectionId。但是当我要使用 CallConnectionID 停止转录时,它会出现以下错误。
[HttpPost("stop-transcription")]
public async Task<IActionResult> StopTranscription([FromBody] StopRecordingRequest request)
{
try
{
var callConnection = _callAutomationClient.GetCallConnection(request.CallConnectionId);
var callMedia = callConnection.GetCallMedia();
StopTranscriptionOptions stopOptions = new StopTranscriptionOptions()
{
OperationContext = "stopTranscription"
};
await callMedia.StopTranscriptionAsync(stopOptions);
return Ok(new { message = "Transcription stopped & transcription stopped" });
}
catch (Exception ex)
{
_logger.LogError(ex, "Error occurred while stopping the recording & transcription.");
return BadRequest(new { error = ex.Message });
}
}
{
"error": "Invalid action, Transcription is not active.\r\nStatus: 412 (Precondition Failed)\r\nErrorCode: 8583\r\n\r\nContent:\r\n{\"error\":{\"code\":\"8583\",\"message\":\"Invalid action, Transcription is not active.\"}}\r\n\r\nHeaders:\r\nDate: Sat, 11 Jan 2025 05:15:57 GMT\r\nConnection: keep-alive\r\nX-Microsoft-Skype-Client: REDACTED\r\nx-ms-client-request-id: 08da2a4c-afcd-469e-827f-b04158f829ad\r\nX-Microsoft-Skype-Chain-ID: REDACTED\r\nx-azure-ref: REDACTED\r\nStrict-Transport-Security: REDACTED\r\nX-Cache: REDACTED\r\nContent-Type: application/json; charset=utf-8\r\nContent-Length: 82\r\n"
}
当我试图阻止转录时,转录似乎没有激活,但我不确定为什么。您能帮我找出问题所在吗?我是否缺少任何步骤或配置?
您遇到的错误,“无效操作,转录未激活”,指当您尝试停止转录时当前未运行转录。发生这种情况的原因可能是转录未成功开始。 202 状态表示请求已接受,但不保证成功并且呼叫应该处于活动状态。
请参阅此 doc,使用 Azure 通信服务操作方法文档将实时转录添加到您的应用程序中
var app = builder.Build();
app.UseWebSockets();
app.Map("/ws", async context =>
{
if (context.WebSockets.IsWebSocketRequest)
{
using var webSocket = await context.WebSockets.AcceptWebSocketAsync();
var buffer = new byte[1024 * 4];
WebSocketReceiveResult result;
do
{
result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), context.RequestAborted);
if (result.MessageType == WebSocketMessageType.Text)
{
var message = Encoding.UTF8.GetString(buffer, 0, result.Count);
Console.WriteLine($"Received: {message}");
}
} while (!result.CloseStatus.HasValue);
await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, context.RequestAborted);
}
else
{
context.Response.StatusCode = 400;
}
});
app.Run();
[HttpPost("start-transcription")]
public async Task<IActionResult> StartTranscription([FromBody] StartRecordingRequest request)
{
try
{
var websocketUri = callbackUriHost.Replace("https", "wss") + "ws";
_logger.LogInformation($"WebSocket URL: {websocketUri}");
var callInvite = new GroupCallLocator(request.GroupCallId);
var connectOptions = new ConnectCallOptions(callInvite, callbackUri)
{
CallIntelligenceOptions = new CallIntelligenceOptions
{
CognitiveServicesEndpoint = new Uri(_cognitiveServicesEndpoint)
},
TranscriptionOptions = new TranscriptionOptions(
new Uri(websocketUri),
"en-US",
false,
TranscriptionTransport.Websocket
),
};
var createCallResult = _callAutomationClient.ConnectCall(connectOptions);
var callConnectionMedia = createCallResult.CallConnection.GetCallMedia();
StartTranscriptionOptions startOptions = new StartTranscriptionOptions
{
Locale = "en-US",
OperationContext = "startMediaStreamingContext"
};
await callConnectionMedia.StartTranscriptionAsync(startOptions);
_logger.LogInformation("Real-time transcription started.");
return Ok(new { CallConnectionId = createCallResult.CallConnection.CallConnectionId });
}
catch (Exception ex)
{
_logger.LogError(ex, "Error starting transcription.");
return BadRequest(new { error = ex.Message });
}
}
The error you’re encountering, **“Invalid action, Transcription is not active,”** suggests that transcription is not currently running when you attempt to stop it. This can occur due to reason pf Transcription Was Not Successfully Started .The 202 status indicates the request was accepted but doesn't guarantee success.
用于启动转录的后端API
[HttpPost("stop-transcription")]
public async Task<IActionResult> StopTranscription([FromBody] StopRecordingRequest request)
{
try
{
var callConnection = _callAutomationClient.GetCallConnection(request.CallConnectionId);
var callMedia = callConnection.GetCallMedia();
StopTranscriptionOptions stopOptions = new StopTranscriptionOptions
{
OperationContext = "stopTranscription"
};
await callMedia.StopTranscriptionAsync(stopOptions);
_logger.LogInformation("Real-time transcription stopped.");
return Ok(new { message = "Transcription stopped successfully." });
}
catch (Exception ex)
{
_logger.LogError(ex, "Error stopping transcription.");
return BadRequest(new { error = ex.Message });
}
}
用于停止转录的后端 API
_callAutomationClient.GetEventProcessor().AttachOngoingEventProcessor<TranscriptionStarted>(
createCallResult.CallConnection.CallConnectionId,
(TranscriptionStarted transcriptionEvent) =>
{
_logger.LogInformation($"Transcription started: CorrelationId={transcriptionEvent.CorrelationId}");
return Task.CompletedTask;
});
_callAutomationClient.GetEventProcessor().AttachOngoingEventProcessor<TranscriptionFailed>(
createCallResult.CallConnection.CallConnectionId,
(TranscriptionFailed transcriptionEvent) =>
{
_logger.LogError($"Transcription failed: CorrelationId={transcriptionEvent.CorrelationId}, Message={transcriptionEvent?.ResultInformation?.Message}");
return Task.CompletedTask;
});
输出: