VS2005 + Windows Mobile 5.0平台
使用coredll.dll中的函数来采集和播放声音。
[DllImport("coredll.dll")]
public static extern int waveOutOpen(out IntPtr hWaveOut,int uDeviceID,WAVEFORMATEX lpFormat,waveOutProc dwCallback,int dwInstance,int dwFlags);
[DllImport("coredll.dll")]
public static extern int waveOutReset(IntPtr hWaveOut);
[DllImport("coredll.dll")]
public static extern int waveOutUnprepareHeader(IntPtr hWaveOut,IntPtr lpWaveOutHdr,int uSize);
[DllImport("coredll.dll")]
public static extern int waveOutClose(IntPtr hWaveOut);
构造函数:
///
/// Default constructor.
///
/// Output device.
/// Sample rate, in samples per second (hertz). For PCM common values are
/// 8.0 kHz, 11.025 kHz, 22.05 kHz, and 44.1 kHz.
/// Bits per sample. For PCM 8 or 16 are the only valid values.
/// Number of channels.
/// Is raised when outputDevice is null.
/// Is raised when any of the aruments has invalid value.
public WaveOut(WavOutDevice outputDevice,int samplesPerSec,int bitsPerSample,int channels)
{
if(outputDevice == null){
throw new ArgumentNullException("outputDevice");
}
if(samplesPerSec = 8000.");
}
if(bitsPerSample = 8.");
}
if(channels = 1.");
}
m_pOutDevice= outputDevice;
m_SamplesPerSec = samplesPerSec;
m_BitsPerSample = bitsPerSample;
m_Channels= channels;
m_BlockSize= m_Channels * (m_BitsPerSample / 8);
m_pPlayItems= new List ();
// 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() + ".");
}
}
///
/// Default destructor.
///
~WaveOut()
{
Dispose();
}
///
/// Cleans up any resources being used.
///
public void Dispose()
{
if(m_IsDisposed){
return;
}
m_IsDisposed = true;
try{
// If playing, we need to reset wav device first.
WavMethods.waveOutReset(m_pWavDevHandle);
// If there are unprepared wav headers, we need to unprepare these.
foreach(PlayItem item in m_pPlayItems){
WavMethods.waveOutUnprepareHeader(m_pWavDevHandle,item.HeaderHandle.AddrOfPinnedObject(),Marshal.SizeOf(item.Header));
item.Dispose();
}
// Close output device.
WavMethods.waveOutClose(m_pWavDevHandle);
m_pOutDevice= null;
m_pWavDevHandle = IntPtr.Zero;
m_pPlayItems= null;
m_pWaveOutProc= null;
}
catch{
}
}
此代码是在非智能设备的语音通讯LumiSoft.Net开源代码里挑选出来的,将全部的“[DllImport("winmm.dll")]”换成了“[DllImport("coredll.dll")]”,在智能设备上运行成功,并能实现语音通话的功能。但就是关闭程序后,删除不了程序文件,说“文件出现共享冲突,正在被使用”等等。我想问问到底是那个地方有问题?
我在一个最简单的例子程序中只实例化了一个WaveOut,然后关掉。
private WaveOut m_pWaveOut= null;
m_pWaveOut = new WaveOut(WaveOut.Devices[0],8000,16,1);
m_pWaveOut. Dispose();
退出程序后,不能删除该程序的执行文件。
初步推断是一些非智能设备的函数在CF中失效,但由于本人经验尚浅,不知道具体是哪个地方出问题了。
|