本文介绍了如何使用线程在两个或三个外部声音声场中同时播放声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好
我想同时在两个或三个外部声卡中播放声音文件,我认为使用线程是解决方案,但我真的不知道如何在播放代码中使用它.这是按一下按钮播放的事件
Hello
I want to playback a sound file in two or three external sound cards at the same time and I think that using threads is the solution but I really didn''t know how to use it in the playback code. This is the event makes on button play
<br />
public partial class PlaybackForm : Form<br />
{<br />
IWavePlayer waveOut;<br />
string fileName = null;<br />
WaveStream mainOutputStream;<br />
WaveChannel32 volumeStream;<br />
int _deviceNum;<br />
int _deviceNum1;<br />
Thread t1;<br />
Thread t2;<br />
public PlaybackForm(int deviceNum,int deviceNum1)<br />
{<br />
InitializeComponent();<br />
_deviceNum = deviceNum;<br />
_deviceNum1 = deviceNum1;<br />
}<br />
private void buttonPlay_Click(object sender, EventArgs e)<br />
{<br />
if (waveOut != null)<br />
{<br />
if (waveOut.PlaybackState == PlaybackState.Playing)<br />
{<br />
return;<br />
}<br />
else if (waveOut.PlaybackState == PlaybackState.Paused)<br />
{<br />
waveOut.Play();<br />
return;<br />
}<br />
}<br />
// we are in a stopped state<br />
// TODO: only re-initialise if necessary<br />
if (String.IsNullOrEmpty(fileName))<br />
{<br />
toolStripButtonOpenFile_Click(sender, e);<br />
}<br />
if (String.IsNullOrEmpty(fileName))<br />
{<br />
return;<br />
}<br />
try<br />
{<br />
CreateWaveOut();<br />
}<br />
catch (Exception driverCreateException)<br />
{<br />
MessageBox.Show(String.Format("{0}", driverCreateException.Message));<br />
return;<br />
}<br />
mainOutputStream = CreateInputStream(fileName);<br />
trackBarPosition.Maximum = (int)mainOutputStream.TotalTime.TotalSeconds;<br />
labelTotalTime.Text = String.Format("{0:00}:{1:00}", (int)mainOutputStream.TotalTime.TotalMinutes,<br />
mainOutputStream.TotalTime.Seconds);<br />
trackBarPosition.TickFrequency = trackBarPosition.Maximum / 30;<br />
try<br />
{<br />
waveOut.Init(mainOutputStream);<br />
}<br />
catch (Exception initException)<br />
{<br />
MessageBox.Show(String.Format("{0}", initException.Message), "Error Initializing Output");<br />
return;<br />
}<br />
// not doing Volume on IWavePlayer any more<br />
volumeStream.Volume = volumeSlider1.Volume;<br />
waveOut.Play();<br />
}<br />
这就是创建waveout的方法
And this is how to create the waveout
<br />
private void CreateWaveOut()<br />
{<br />
CloseWaveOut();<br />
int latency = (int)comboBoxLatency.SelectedItem;<br />
//if (radioButtonWaveOut.Checked)<br />
{<br />
//WaveCallbackInfo callbackInfo = checkBoxWaveOutWindow.Checked ?<br />
WaveCallbackInfo callbackInfo = WaveCallbackInfo.FunctionCallback();<br />
// WaveCallbackInfo callbackInfo = WaveCallbackInfo.FunctionCallback();<br />
// WaveCallbackInfo.NewWindow(): WaveCallbackInfo.FunctionCallback();<br />
WaveOut outputDevice = new WaveOut(callbackInfo);<br />
outputDevice.DesiredLatency = latency;<br />
outputDevice.DeviceNumber = _deviceNum;<br />
waveOut = outputDevice;<br />
}<br />
}<br />
我声明了两个deviceNum,但是直到现在我只能在一个设备上播放声音,这就是为什么要使用线程的原因.您能帮我吗,请提前谢谢您.
I declared two deviceNum but until now I can playsound only in one device,that''s why I want to use thread. Can you help me please Thank you in advance
推荐答案
这篇关于如何使用线程在两个或三个外部声音声场中同时播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!