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

使用C#的NAudio库同步播放多轨MP3的问题求助

解决NAudio多音频文件同步播放问题

先修正代码中的低级错误

你的代码里存在明显的初始化错误,这会直接导致播放内容混乱:

// 错误:应传入audioFile3而非audioFile2
outputDevice3.Init(audioFile2);
// 错误:应传入audioFile4而非audioFile2
outputDevice4.Init(audioFile2);

先将其修正为对应audioFile3和audioFile4,再处理核心同步问题。

根本问题:多WaveOutEvent的调度延迟

即使修正上述错误,使用多个WaveOutEvent实例播放仍会出现同步偏差——每个实例对应独立的音频播放线程,系统线程调度的微小延迟会被放大,完全无法满足你对50-100ms级别的同步敏感度要求。

正确的解决思路是将所有单声道音频合并为一个多声道音频流,使用单个WaveOutEvent播放,这样所有轨道会在同一个播放线程中同步输出,彻底消除时序偏差。

修改后的实现代码

以下是基于NAudio的同步播放实现,通过MultiplexingSampleProvider将4个单声道流合并为4声道流,用单个输出设备播放:

using NAudio.Wave;
using NAudio_Test.Properties;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace NAudio_Test
{
    public partial class Form1 : Form
    {
        private WaveOutEvent _outputDevice;
        private List<AudioFileReader> _audioFiles = new List<AudioFileReader>();
        private ISampleProvider _mixedSampleProvider;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            lbDirectory.Text = Settings.Default.StartingDirectory;
            lbFile.Text = Settings.Default.File;
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            _outputDevice?.Stop();
        }

        private void OnPlaybackStopped(object sender, StoppedEventArgs args)
        {
            _outputDevice?.Dispose();
            _outputDevice = null;

            foreach (var file in _audioFiles)
            {
                file.Dispose();
            }
            _audioFiles.Clear();
            _mixedSampleProvider = null;
        }

        private void btnPlay_Click(object sender, EventArgs e)
        {
            if (_outputDevice != null) return;

            // 加载4个音频文件
            var filePaths = new[]
            {
                $@"{lbDirectory.Text}\{lbFile.Text}_1.mp3",
                $@"{lbDirectory.Text}\{lbFile.Text}_2.mp3",
                $@"{lbDirectory.Text}\{lbFile.Text}_3.mp3",
                $@"{lbDirectory.Text}\{lbFile.Text}_4.mp3"
            };

            List<ISampleProvider> sampleProviders = new List<ISampleProvider>();
            foreach (var path in filePaths)
            {
                var audioFile = new AudioFileReader(path);
                _audioFiles.Add(audioFile);
                // 将音频转换为SampleProvider,确保格式统一
                sampleProviders.Add(audioFile.ToSampleProvider());
            }

            // 将4个单声道合并为4声道流
            _mixedSampleProvider = new MultiplexingSampleProvider(sampleProviders, 4);
            // 绑定输入轨道到对应输出声道
            for (int i = 0; i < sampleProviders.Count; i++)
            {
                _mixedSampleProvider.ConnectInputToOutput(i, i);
            }

            // 初始化单个输出设备
            _outputDevice = new WaveOutEvent();
            _outputDevice.PlaybackStopped += OnPlaybackStopped;
            _outputDevice.Init(_mixedSampleProvider);

            // 启动同步播放
            _outputDevice.Play();
        }
    }
}

额外说明

  • 如果你的音频文件采样率、位深度不一致,需要先通过SampleProviderConverter统一格式,否则MultiplexingSampleProvider会抛出异常。
  • 若需将4个单声道混合为立体声输出,可改用MixingSampleProvider,将所有单声道混合到左右声道。
  • 单个输出设备的播放模式从根本上避免了多线程调度带来的同步偏差,完全能满足你对50-100ms级别的同步要求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 04:15:47