
关于
Azure AI Voice Live .NET SDK。使用双向 WebSocket 通信构建实时语音 AI 应用。
name: azure-ai-voicelive-dotnet description: Azure AI Voice Live SDK for .NET。构建具有双向 WebSocket 通信的实时语音 AI 应用。 risk: unknown source: community date_added: '2026-02-27'
Azure.AI.VoiceLive (.NET)
用于构建双向语音助手的实时语音 AI SDK。
安装
dotnet add package Azure.AI.VoiceLive
dotnet add package Azure.Identity
dotnet add package NAudio # 用于音频捕获/播放
当前版本:稳定版 v1.0.0,预览版 v1.1.0-beta.1
环境变量
AZURE_VOICELIVE_ENDPOINT=https://<resource>.services.ai.azure.com/
AZURE_VOICELIVE_MODEL=gpt-4o-realtime-preview
AZURE_VOICELIVE_VOICE=en-US-AvaNeural
# 可选:如果不使用 Entra ID 则使用 API 密钥
AZURE_VOICELIVE_API_KEY=<your-api-key>
身份验证
Microsoft Entra ID(推荐)
using Azure.Identity;
using Azure.AI.VoiceLive;
Uri endpoint = new Uri("https://your-resource.cognitiveservices.azure.com");
DefaultAzureCredential credential = new DefaultAzureCredential();
VoiceLiveClient client = new VoiceLiveClient(endpoint, credential);
所需角色:Cognitive Services User(在 Azure 门户 → 访问控制中分配)
API 密钥
Uri endpoint = new Uri("https://your-resource.cognitiveservices.azure.com");
AzureKeyCredential credential = new AzureKeyCredential("your-api-key");
VoiceLiveClient client = new VoiceLiveClient(endpoint, credential);
客户端层次结构
VoiceLiveClient
└── VoiceLiveSession(WebSocket 连接)
├── ConfigureSessionAsync()
├── GetUpdatesAsync() → SessionUpdate 事件
├── AddItemAsync() → UserMessageItem, FunctionCallOutputItem
├── SendAudioAsync()
└── StartResponseAsync()
核心工作流
1. 启动会话并配置
using Azure.Identity;
using Azure.AI.VoiceLive;
var endpoint = new Uri(Environment.GetEnvironmentVariable("AZURE_VOICELIVE_ENDPOINT"));
var client = new VoiceLiveClient(endpoint, new DefaultAzureCredential());
var model = "gpt-4o-mini-realtime-preview";
// 启动会话
using VoiceLiveSession session = await client.StartSessionAsync(model);
// 配置会话
VoiceLiveSessionOptions sessionOptions = new()
{
Model = model,
Instructions = "You are a helpful AI assistant. Respond naturally.",
Voice = new AzureStandardVoice("en-US-AvaNeural"),
TurnDetection = new AzureSemanticVadTurnDetection()
{
Threshold = 0.5f,
PrefixPadding = TimeSpan.FromMilliseconds(300),
SilenceDuration = TimeSpan.FromMilliseconds(500)
},
InputAudioFormat = InputAudioFormat.Pcm16,
OutputAudioFormat = OutputAudioFormat.Pcm16
};
// 设置模态(语音助手同时使用文本和音频)
sessionOptions.Modalities.Clear();
sessionOptions.Modalities.Add(InteractionModality.Text);
sessionOptions.Modalities.Add(InteractionModality.Audio);
await session.ConfigureSessionAsync(sessionOptions);
2. 处理事件
await foreach (SessionUpdate serverEvent in session.GetUpdatesAsync())
{
switch (serverEvent)
{
case SessionUpdateResponseAudioDelta audioDelta:
byte[] audioData = audioDelta.Delta.ToArray();
// 通过 NAudio 或其他音频库播放音频
break;
case SessionUpdateResponseTextDelta textDelta:
Console.Write(textDelta.Delta);
break;
case SessionUpdateResponseFunctionCallArgumentsDone functionCall:
// 处理函数调用(参见函数调用部分)
break;
case SessionUpdateError error:
Console.WriteLine($"Error: {error.Error.Message}");
break;
case SessionUpdateResponseDone:
Console.WriteLine("\n--- 响应完成 ---");
break;
}
}
3. 发送用户消息
await session.AddItemAsync(new UserMessageItem("Hello, can you help me?"));
await session.StartResponseAsync();
4. 函数调用
// 定义函数
var weatherFunction = new VoiceLiveFunctionDefinition("get_current_weather")
{
Description = "Get the current weather for a given location",
Parameters = BinaryData.FromString("""
{
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state or country"
}
},
"required": ["location"]
}
""")
};
// 添加到会话选项
sessionOptions.Tools.Add(weatherFunction);
// 在事件循环中处理函数调用
if (serverEvent is SessionUpdateResponseFunctionCallArgumentsDone functionCall)
{
if (functionCall.Name == "get_current_weather")
{
var parameters = JsonSerializer.Deserialize<Dictionary<string, string>>(functionCall.Arguments);
string location = parameters["location"];
// 调用你的天气 API
string result = await GetWeatherAsync(location);
// 将结果发送回会话
await session.AddItemAsync(new FunctionCallOutputItem(functionCall.CallId, result));
await session.StartResponseAsync();
}
}
兼容工具
Claude CodeCursor
标签
AI与机器学习