/// <summary> /// Opens the device for writing with the specified format. /// </summary> /// <param name="waveFormat">The format of the device to open.</param> public void Open(WaveFormat waveFormat) { lock (this.startStopLock) { if (this.handle != null) { throw new InvalidOperationException("The device is already open."); } WAVEFORMATEX wfx = new WAVEFORMATEX(); wfx.nAvgBytesPerSec = waveFormat.AverageBytesPerSecond; wfx.wBitsPerSample = waveFormat.BitsPerSample; wfx.nBlockAlign = waveFormat.BlockAlign; wfx.nChannels = waveFormat.Channels; wfx.wFormatTag = (short)(int)waveFormat.FormatTag; wfx.nSamplesPerSec = waveFormat.SamplesPerSecond; wfx.cbSize = 0; this.recordingFormat = waveFormat.Clone(); IntPtr tempHandle = new IntPtr(); NativeMethods.Throw( NativeMethods.waveInOpen( ref tempHandle, this.deviceId, ref wfx, this.callback, (IntPtr)0, WaveOpenFlags.CALLBACK_FUNCTION | WaveOpenFlags.WAVE_FORMAT_DIRECT), NativeMethods.ErrorSource.WaveOut); this.handle = new WaveInSafeHandle(tempHandle); } }
/// <summary> /// Closes the device. If the device is playing, playback is stopped. /// </summary> /// <remarks> /// If the device is not currently open, this function does nothing. /// </remarks> public void Close() { lock (this.startStopLock) { if (this.handle != null) { if (!this.handle.IsClosed && !this.handle.IsInvalid) { this.Stop(); this.handle.Close(); } this.handle = null; } } }