如何在许多设备上同时播放声音

时间:2011-05-15 21:52:12

标签: c# audio naudio

我想同时在三个外部声卡中播放声音,我的意思是当我点击一个按钮时,我可以听到三个与三个声卡相关的扬声器发出的声音。 我有一个功能,但它只在一个设备中播放声音,它找到的第一个,我的意思是在这个代码中第一个设备是数字0,所以它在其中播放声音,但如果你首先写设备号1,它将在其中播放声音,作为结论它只在一个设备中播放声音,它不能同时适用于所有设备。 这是它的代码:

public void playAllAvailableDevices()
{
//create a new class for each wav file & output etc.
WaveOut waveOut1 = new WaveOut();
WaveFileReader waveReader1 = new WaveFileReader(fileName);
WaveOut waveOut2 = new WaveOut();
WaveFileReader waveReader2 = new WaveFileReader(fileName);
WaveOut waveOut3 = new WaveOut();
WaveFileReader waveReader3 = new WaveFileReader(fileName);

switch (waveOutDevices)
{
case 1: 
waveOut1.Init(waveReader1);
waveOut1.DeviceNumber = 0;
waveOut1.Play();
break;
case 2: 
waveOut1.Init(waveReader1);
waveOut1.DeviceNumber = 0;
waveOut1.Play();

waveOut2.Init(waveReader2);
waveOut2.DeviceNumber = 1;
waveOut2.Play();
break;
case 3:
waveOut1.Init(waveReader1);
waveOut1.DeviceNumber = 0;
waveOut1.Play();

waveOut2.Init(waveReader2);
waveOut2.DeviceNumber = 1;
waveOut2.Play();

waveOut3.Init(waveReader3);
waveOut3.DeviceNumber = 2;
waveOut3.Play();
break;
}}

fileName是我要播放的声音文件的名称,在我的代码中我从darabase获取此名称:

private void btnAlarm1_Click(object sender, EventArgs e)
    {

        OdbcConnection cn = new OdbcConnection("DSN=cp1");
        cn.Open();
        OdbcCommand cmd1 = new OdbcCommand("select chemin from alarme where code_alarme=41", cn);
        cmd1.Connection = cn;
        fileName = cmd1.ExecuteScalar().ToString();
        wave = new WaveOut();
        playAllAvailableDevices();
    }

你能帮我找到解决方案吗???? 先感谢您。 美好的一天。

2 个答案:

答案 0 :(得分:7)

在调用DeviceNumber之前,您需要在WaveOut对象上设置Init属性。您可以使用一个简单的函数来清理代码:

    private void PlaySoundInDevice(int deviceNumber, string fileName)
    {
        if (outputDevices.ContainsKey(deviceNumber))
        {
            outputDevices[deviceNumber].WaveOut.Dispose();
            outputDevices[deviceNumber].WaveStream.Dispose();
        }
        var waveOut = new WaveOut();
        waveOut.DeviceNumber = deviceNumber;
        WaveStream waveReader = new WaveFileReader(fileName);
        // hold onto the WaveOut and  WaveStream so we can dispose them later
        outputDevices[deviceNumber] = new PlaybackSession { WaveOut=waveOut, WaveStream=waveReader };

        waveOut.Init(waveReader);
        waveOut.Play();
    }

    private Dictionary<int, PlaybackSession> outputDevices = new Dictionary<int, PlaybackSession>();

    class PlaybackSession
    {
        public IWavePlayer WaveOut { get; set; }
        public WaveStream WaveStream { get; set; }
    }

字典保存在WaveOut上,因此在播放过程中不会收集垃圾,并允许我们正确清理。在退出应用程序之前,请确保正确清理:

    private void DisposeAll()
    {
        foreach (var playbackSession in outputDevices.Values)
        {
            playbackSession.WaveOut.Dispose();
            playbackSession.WaveStream.Dispose();
        }
    }

现在,您可以使用for循环播放所有可用设备,而不是需要重复代码的switch语句:

    public void PlayInAllAvailableDevices(string fileName)
    {
        int waveOutDevices = WaveOut.DeviceCount;
        for (int n = 0; n < waveOutDevices; n++)
        {
            PlaySoundInDevice(n, fileName);
        }
    }

答案 1 :(得分:1)

我认为你要找的是BASS音频库:

http://www.un4seen.com/

这可能也有效:

http://www.alvas.net/alvas.audio.aspx

我不认为如果没有像上面列出的那样的第三方加载项,C#就会设置为这样做。也许比我聪明的人可以帮助你让它运作但如果没有,这些图书馆将帮助你走上你想要的道路。