如何在播放函数中使用线程

本文关键字:线程 函数 播放 | 更新日期: 2023-09-27 17:59:09

我想同时在两到三个外部声卡中播放声音文件,我认为使用线程是解决方案,但我真的不知道如何在播放代码中使用它。这是按钮播放的事件:

public partial class PlaybackForm : Form
{
    IWavePlayer waveOut;
    string fileName = null;
    WaveStream mainOutputStream;
    WaveChannel32 volumeStream;
    int _deviceNum;
    int _deviceNum1;
    Thread t1;
    Thread t2;
    public PlaybackForm(int deviceNum,int deviceNum1)
    {
        InitializeComponent();
        _deviceNum = deviceNum;
        _deviceNum1 = deviceNum1;
    }

    private void buttonPlay_Click(object sender, EventArgs e)
    {
        if (waveOut != null)
        {
            if (waveOut.PlaybackState == PlaybackState.Playing)
            {
              return;
            }
            else if (waveOut.PlaybackState == PlaybackState.Paused)
            {
                waveOut.Play();
                return;
            }
        }
        // we are in a stopped state
        // TODO: only re-initialise if necessary
        if (String.IsNullOrEmpty(fileName))
        {
            toolStripButtonOpenFile_Click(sender, e);
        }
        if (String.IsNullOrEmpty(fileName))
        {
            return;
        }
        try
        {
            CreateWaveOut();
        }
        catch (Exception driverCreateException)
        {
            MessageBox.Show(String.Format("{0}", driverCreateException.Message));
            return;
        }
        mainOutputStream = CreateInputStream(fileName);
        trackBarPosition.Maximum = (int)mainOutputStream.TotalTime.TotalSeconds;
        labelTotalTime.Text = String.Format("{0:00}:{1:00}", (int)mainOutputStream.TotalTime.TotalMinutes,
            mainOutputStream.TotalTime.Seconds);
        trackBarPosition.TickFrequency = trackBarPosition.Maximum / 30;
        try
        {
            waveOut.Init(mainOutputStream);
        }
        catch (Exception initException)
        {
            MessageBox.Show(String.Format("{0}", initException.Message), "Error Initializing Output");
            return;
        }
        // not doing Volume on IWavePlayer any more
        volumeStream.Volume = volumeSlider1.Volume;
        waveOut.Play();
    }

这就是如何创建波形输出:

  private void CreateWaveOut()
    {
        CloseWaveOut();
        int latency = (int)comboBoxLatency.SelectedItem;
        //if (radioButtonWaveOut.Checked)
        {
            //WaveCallbackInfo callbackInfo = checkBoxWaveOutWindow.Checked ?
            WaveCallbackInfo callbackInfo = WaveCallbackInfo.FunctionCallback();
            // WaveCallbackInfo callbackInfo = WaveCallbackInfo.FunctionCallback();
            // WaveCallbackInfo.NewWindow(): WaveCallbackInfo.FunctionCallback();
            WaveOut outputDevice = new WaveOut(callbackInfo);
            outputDevice.DesiredLatency = latency;
            outputDevice.DeviceNumber = _deviceNum;
            waveOut = outputDevice;
        }
    }

我声明了两个deviceNum,但到目前为止,我只能在一个设备中播放声音,这就是我想使用thread的原因。你能帮帮我吗提前感谢

如何在播放函数中使用线程

这样做:

using System.Threading;
...
private void buttonPlay_Click(object sender, EventArgs e)
{
    ThreadPool.QueueUserWorkItem(new WaitCallback(this.PlaySound), 1);
    ThreadPool.QueueUserWorkItem(new WaitCallback(this.PlaySound), 2);
}
private void PlaySound(object obj)
{
    int deviceNumber = (int)obj;
    // Do the stuff you used to do in buttonPlay_Click
    WaveOut myWaveOut = CreateWaveOut(deviceNumber);
    ...
}
private WaveOut CreateWaveOut(int deviceNumber)
{
    ...
    WaveOut outputDevice = new WaveOut(callbackInfo);
    outputDevice.DesiredLatency = latency;
    outputDevice.DeviceNumber = _deviceNum;
    return outputDevice;
}