C# 修复程序如何打开wav设备LumiSoft.Net失败?
我将此代码用于打开wave设备,并获得异常“无法打开wave设备”。当我重置计算机时,它不会给出异常,并且在我第一次运行程序时,它是正常的,但在每次运行结束后都会出现异常C# 修复程序如何打开wav设备LumiSoft.Net失败?,c#,.net,voice,lumisoft,C#,.net,Voice,Lumisoft,我将此代码用于打开wave设备,并获得异常“无法打开wave设备”。当我重置计算机时,它不会给出异常,并且在我第一次运行程序时,它是正常的,但在每次运行结束后都会出现异常 public WaveOut(WavOutDevice outputDevice,int samplesPerSec,int bitsPerSample,int channels) { if(outputDevice == null){ throw new
public WaveOut(WavOutDevice outputDevice,int samplesPerSec,int bitsPerSample,int channels)
{
if(outputDevice == null){
throw new ArgumentNullException("outputDevice");
}
if(samplesPerSec < 8000){
throw new ArgumentException("Argument 'samplesPerSec' value must be >= 8000.");
}
if(bitsPerSample < 8){
throw new ArgumentException("Argument 'bitsPerSample' value must be >= 8.");
}
if(channels < 1){
throw new ArgumentException("Argument 'channels' value must be >= 1.");
}
m_pOutDevice = outputDevice;
m_SamplesPerSec = samplesPerSec;
m_BitsPerSample = bitsPerSample;
m_Channels = channels;
m_BlockSize = m_Channels * (m_BitsPerSample / 8);
m_pPlayItems = new List<PlayItem>();
// Try to open wav device.
WAVEFORMATEX format = new WAVEFORMATEX();
format.wFormatTag = WavFormat.PCM;
format.nChannels = (ushort)m_Channels;
format.nSamplesPerSec = (uint)samplesPerSec;
format.nAvgBytesPerSec = (uint)(m_SamplesPerSec * m_Channels * (m_BitsPerSample / 8));
format.nBlockAlign = (ushort)m_BlockSize;
format.wBitsPerSample = (ushort)m_BitsPerSample;
format.cbSize = 0;
// We must delegate reference, otherwise GC will collect it.
m_pWaveOutProc = new waveOutProc(this.OnWaveOutProc);
int result = WavMethods.waveOutOpen(out m_pWavDevHandle,m_pOutDevice.Index,format,m_pWaveOutProc,0,WavConstants.CALLBACK_FUNCTION);
if(result != MMSYSERR.NOERROR){
throw new Exception("Failed to open wav device, error: " + result.ToString() + ".");
}
}
公共WaveOut(WavOutDevice-outputDevice、int-samplesPerSec、int-bitsPerSample、int-Channel)
{
if(outputDevice==null){
抛出新的ArgumentNullException(“outputDevice”);
}
如果(样本数小于8000){
抛出新ArgumentException(“参数'samplesPerSec'值必须大于等于8000”);
}
if(位采样<8){
抛出新ArgumentException(“参数'bitsPerSample'值必须大于等于8”);
}
中频(通道<1){
抛出新ArgumentException(“参数“通道”值必须大于等于1”);
}
m_pOutDevice=输出设备;
m_SamplesPerSec=SamplesPerSec;
m_BitsPerSample=BitsPerSample;
m_通道=通道;
m_BlockSize=m_通道*(m_比特采样/8);
m_pPlayItems=新列表();
//尝试打开wav设备。
WAVEFORMATEX format=新的WAVEFORMATEX();
format.wFormatTag=WavFormat.PCM;
format.nChannels=(ushort)m_通道;
format.nSamplesPerSec=(uint)samplesPerSec;
format.nAvgBytesPerSec=(uint)(m_samplesperc*m_Channels*(m_BitsPerSample/8));
format.nblockallign=(ushort)m_块大小;
format.wBitsPerSample=(ushort)m_BitsPerSample;
format.cbSize=0;
//我们必须委托引用,否则GC将收集它。
m_pWaveOutProc=新的waveOutProc(this.OnWaveOutProc);
int result=WavMethods.waveOutOpen(out m_pWavDevHandle,m_pOutDevice.Index,format,m_pWaveOutProc,0,WavConstants.CALLBACK_函数);
如果(结果!=MMSYSERR.NOERROR){
抛出新异常(“未能打开wav设备,错误:+result.ToString()+”);
}
}
尽管我对c#中的wav设备几乎没有经验,但我认为您的问题与程序关闭时未关闭设备有关
你需要一个像这样的电话
WavMethods.waveOutClose(m_pWavDevHandle);
在应用程序退出之前(如果waveOutoOpen调用失败,显然不需要)