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

NAudio输入设备无法接收有效音频数据问题求助

问题:NAudio在.NET 8下录制WAV仅采集到无效数据(0x00/0xFF)

环境与场景

  • Windows 11系统,安装BitDefender杀毒软件
  • 应用从.NET Framework 4.8迁移至.NET 8后出现录制异常
  • 使用NAudio实现麦克风录制WAV文件,DataAvailable事件正常触发,但返回数据仅为0x00、0xFF这类无效值(日志示例:字节数17640,缓冲区片段:00, 00, 00, 00, FF, FF, 01, 00, 00, 00)

核心实现代码

private MMDevice SetCurrentDevice(DataFlow dataFlow, string deviceName)
{
    var devices = m_deviceEnumerator.EnumerateAudioEndPoints(dataFlow, DeviceState.Active);

    MMDevice selectedDevice = null;

    for (int i = 0; i < devices.Count; i++)
    {
        var device = devices[i];
        if (CompareDeviceNameStrings(device.DeviceFriendlyName, deviceName))
        {
            selectedDevice = device;

            if (dataFlow == DataFlow.Capture)
            {
                m_currentMicIndex = i;
            }
            else
            {
                m_currentSpeakerIndex = i;
            }

            MuteAllDevices(dataFlow);
            break;
        }
    }

    return selectedDevice;
}

public void Record(String strFile)
{
    try
    {
        SetMicVolume(100);
        m_bIsRecording = true;

        m_strCurrentFile = strFile;
        m_WaveSource = new WaveIn();
        int waveInDevice = AudioDeviceCtrl.GetInstance().GetCurrentMicIndex();

        if (waveInDevice < 0)
        {
            SimpleLog.Error("No microphone device found.");
            return;
        }
        m_WaveSource.DeviceNumber = waveInDevice;

        WaveInCapabilities deviceInfo = WaveIn.GetCapabilities(waveInDevice);            
        
        m_WaveSource.WaveFormat = new WaveFormat(44100, 2);

        m_WaveSource.DataAvailable += new EventHandler<WaveInEventArgs>(DataAvailable);
        m_WaveSource.RecordingStopped += new EventHandler<StoppedEventArgs>(RecordingStopped);

        m_WaveFile = new WaveFileWriter(strFile, m_WaveSource.WaveFormat);

        m_WaveSource.StartRecording();
    }
    catch (Exception ex)
    {
        SimpleLog.Error($"Recording failed: {ex.Message}");
    }
}

void DataAvailable(object sender, WaveInEventArgs e)
{
    m_WaveFile.Write(e.Buffer, 0, e.BytesRecorded);
    m_WaveFile.Flush();

    SimpleLog.Debug("bytecound: " + e.BytesRecorded.ToString() );
    int bytesToLog = Math.Min(e.BytesRecorded, 10); // Log first 10 bytes or fewer if less available
    string bufferSnippet = string.Join(", ", e.Buffer.Take(bytesToLog).Select(b => b.ToString("X2")));
    SimpleLog.Debug("Buffer snippet: " + bufferSnippet);
}

已完成的排查动作

  • Windows系统设置中,捕获设备对声音有正常反馈,硬件本身无问题
  • 录制前通过SetMicVolume(100)将目标设备取消静音并设置100%音量,操作已生效
  • 检查BitDefender日志,仅记录麦克风访问行为,未发现拦截动作
  • 更换不同麦克风设备后,问题依然存在
  • 确认NAudio官方标注支持.NET 6+,怀疑版本兼容性可能是诱因

排查方向提示

  • 验证NAudio版本兼容性:确保使用适配.NET 8的最新稳定版NAudio(如2.2.1及以上),旧版本在.NET 8环境下可能存在未适配的API调用问题
  • 替换采集API:.NET Core/.NET 5+环境下,WasapiCapture的兼容性优于WaveIn,尝试改用WasapiCapture实现基础录制,验证是否能采集到有效数据
  • 匹配设备原生格式:不要硬编码WaveFormat(44100, 2),通过WaveIn.GetCapabilities获取设备支持的格式列表,或从MMDevice获取默认格式,使用设备兼容的参数进行录制
  • 核对设备索引一致性:.NET 8与.NET Framework下音频设备的枚举顺序可能存在差异,确认GetCurrentMicIndex()返回的索引与WaveIn枚举的设备顺序完全匹配
  • 检查权限机制变化:.NET 8下麦克风权限的申请逻辑与.NET Framework不同,确认应用已在Windows隐私设置中获取麦克风访问权限
  • 排查设备状态冲突:在Record方法中,通过MMDevice获取当前捕获设备的实时状态,确认设备未被系统或其他进程静音、处于正常活动状态
  • 极简示例测试:编写一个仅包含NAudio基础录制逻辑的.NET 8控制台程序,排除业务代码中其他逻辑的干扰,验证问题是否由核心录制逻辑导致

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 05:17:19