2011-06-15 17 views
1

我想在兩個或三個外部聲卡中同時播放聲音文件,我認爲使用線程是解決方案,但我真的不知道如何使用它在播放代碼中。 這是事件使按鈕玩法:如何在播放功能中使用線程

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

這是如何創建waveout的:

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但到現在爲止,我可以playsound只能在一臺設備,這就是爲什麼我想用線程。 你能幫我請 預先感謝您

回答

0

做這樣的事情:

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

感謝您的回覆,但現在我得到this.PlaySound和waveout的myWaveOut = CreateWaveOut(deviceNumber)例外 – nicolecastel 2011-06-15 17:01:16

+0

和作爲回報outputDevices – nicolecastel 2011-06-15 17:02:49

+0

什麼異常? – Hauzi 2011-06-15 17:19:31

相關問題