我将此代码用于开放波设备,并获得此异常“无法打开波设备”。当我重置计算机时它不会出现异常,并且第一次运行程序时它没关系,但之后每次运行以例外结束。
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() + ".");
}
}
答案 0 :(得分:-1)
Altough我对c#中的wav设备没什么经验,我认为你的问题与程序关闭时不关闭设备有关。
你需要像
这样的电话WavMethods.waveOutClose(m_pWavDevHandle);
在你的应用程序退出之前的某个地方(如果waveOutoOpen调用失败,显然不需要它)