如何在众多的设备在同一时间播放声音 [英] How to play sound in many devices at the same time

查看:198
本文介绍了如何在众多的设备在同一时间播放声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在同一时间在三个外部声卡播放的声音,我的意思是,当我在一个按钮,单击我可以从这些都与我的三个声卡三个扬声器听到声音。
我有一个功能,但它播放声音只能在一台设备,它找到的第一个,我的意思是这个code中的第一个设备编号为0,所以在它播放声音,但如果你在第一次写设备号为1 ,它将在它播放声音,因为它播放声音仅在一个设备中的结论,它不为同时所有设备工程。
这是它的code:

I want to play sound in three external sound cards at the same time,I mean when I click in a button I can hear sound from three speakers which are related to my three sound cards. I have a function but it plays sound only in one device,the first one it finds,I mean in this code the first device is number 0,so it play sound in it,but if you write device number 1 at first,it will play sound in it,as a conclusion it plays sound only in one device,it dont works for all the devices at the same time. This is its code:

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;
}}

文件名是声音文件我要玩的名字,在我的code我从darabase得到这名:

fileName is the name of the sound file I want to play,in my code I get this name from a 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();
    }

您可以帮我找到解决办法,请????
先谢谢你。
美好的一天。

Can you help me to find the solution please???? Thank you in advance. Good day.

推荐答案

您需要在调用之前设置waveout的对象上的 DeviceNumber 属性初始化。你可以通过使用一个简单的函数清理code很多:

You need to set the DeviceNumber property on the WaveOut object before calling Init. You could clean up your code a lot by using a simple function:

    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的所以它不会在播放过程中收集的垃圾,使我们能够正确地进行清理。在退出应用程序时,确保你收拾妥当:

The dictionary holds onto the WaveOut so it doesn't get garbage collected during playback, and allows us to clean up properly. Before you exit your application, make sure you clean up properly:

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

现在,你可以在所有可用设备使用for循环,而不是你的switch语句,需要你复制code玩法:

And now you can play in all available devices using a for loop instead of your switch statement that requires you to duplicate code:

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

这篇关于如何在众多的设备在同一时间播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆