You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置SpeechSynthesizer仅生成音频数据而不播放声音?

解决SpeechSynthesizer仅生成音频不播放的问题

你可以通过给SpeechSynthesizer指定空输出音频配置来实现仅生成音频数据、不播放声音的需求,具体修改如下:

修改后的代码

public async Task<AudioResponse> GenerateAudio()
{
    _speechConfig.SpeechSynthesisVoiceName = $"{request.Language}-{request.Voice}";
    // 创建不输出到任何播放设备的音频配置
    using var audioConfig = AudioConfig.FromNullOutput();
    using SpeechSynthesizer synthesizer = new(_speechConfig, audioConfig);
  
    SpeechSynthesisResult result = await synthesizer.SpeakTextAsync("Test");
    File.WriteAllBytes(@"D:\front-doors-opening.mp3", result.AudioData);
    return new AudioResponse { Audio = result.AudioData };
}

关键说明

  • 使用AudioConfig.FromNullOutput()创建的配置会让SpeechSynthesizer跳过音频播放环节,仅专注于生成音频数据。
  • 原方法缺少async关键字(因使用了await),需要补充并将返回类型改为Task<AudioResponse>,符合异步方法规范。

内容的提问来源于stack exchange,提问作者kanils_

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 12:10:36