Exemple #1
0
        /// <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.");
                }

                NativeMethods.WAVEFORMATEX wfx = new NativeMethods.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;

                IntPtr tempHandle = new IntPtr();
                NativeMethods.Throw(
                    NativeMethods.waveOutOpen(
                        ref tempHandle,
                        (uint)this.deviceId,
                        ref wfx,
                        this.callback,
                        (IntPtr)0,
                        NativeMethods.WAVEOPENFLAGS.CALLBACK_FUNCTION | NativeMethods.WAVEOPENFLAGS.WAVE_FORMAT_DIRECT),
                    NativeMethods.ErrorSource.WaveOut);
                this.handle = new WaveOutSafeHandle(tempHandle);

                lock (this.bufferingLock)
                {
                    this.buffering = true;
                    Monitor.Pulse(this.bufferingLock);
                }

                this.bufferMaintainerThread = new Thread(new ThreadStart(this.MaintainBuffers));
                this.bufferMaintainerThread.IsBackground = true;
                this.bufferMaintainerThread.Name         = "WaveOut MaintainBuffers thread. (DeviceID = " + this.deviceId + ")";
                this.bufferMaintainerThread.Start();
            }
        }
Exemple #2
0
        /// <summary>
        /// Closes the device.  If the device is playing, playback is stopped.
        /// </summary>
        public void Close()
        {
            lock (this.startStopLock)
            {
                if (this.handle != null)
                {
                    if (!this.handle.IsClosed && !this.handle.IsInvalid)
                    {
                        this.Stop();

                        lock (this.bufferingLock)
                        {
                            this.buffering = false;
                            Monitor.Pulse(this.bufferingLock);
                        }

                        this.bufferMaintainerThread.Join();

                        this.handle.Close();
                        this.handle = null;
                    }
                }
            }
        }