/// <summary> /// Pause the audio /// </summary> public void Pause() { _pressStop = false; if (_playbackState == PlaybackState.Playing) { MmResult result; lock (_waveOutLock) { result = WaveInterop.waveOutPause(_hWaveOut); } if (result != MmResult.NoError) { throw new MmException(result, "waveOutPause"); } _playbackState = PlaybackState.Paused; } }
private GCHandle _hThis; // for the user callback /// <summary> /// creates a new wavebuffer /// </summary> /// <param name="hWaveOut">WaveOut device to write to</param> /// <param name="bufferSize">Buffer size in bytes</param> /// <param name="bufferFillStream">Stream to provide more data</param> /// <param name="waveOutLock">Lock to protect WaveOut API's from being called on >1 thread</param> public WaveBuffer(IntPtr hWaveOut, Int32 bufferSize, IWaveProvider bufferFillStream, object waveOutLock) { _bufferSize = bufferSize; _buffer = new byte[bufferSize]; _hBuffer = GCHandle.Alloc(_buffer, GCHandleType.Pinned); _hWaveOut = hWaveOut; _waveStream = bufferFillStream; _waveOutLock = waveOutLock; _header = new WaveHeader(); _hHeader = GCHandle.Alloc(_header, GCHandleType.Pinned); _header.dataBuffer = _hBuffer.AddrOfPinnedObject(); _header.bufferLength = bufferSize; _header.loops = 1; _hThis = GCHandle.Alloc(this); _header.userData = (IntPtr)_hThis; lock (waveOutLock) { MmException.Try(WaveInterop.waveOutPrepareHeader(hWaveOut, _header, Marshal.SizeOf(_header)), "waveOutPrepareHeader"); } }
/// <summary> /// Dispose(bool disposing) executes in two distinct scenarios. /// If disposing equals true, the method has been called directly /// or indirectly by a user's code. Managed and unmanaged resources /// can be disposed. /// If disposing equals false, the method has been called by the /// runtime from inside the finalizer and you should not reference /// other objects. Only unmanaged resources can be disposed. /// </summary> protected virtual void Dispose(bool disposing) { // Check to see if Dispose has already been called. if (!this._disposed) { Stop(); // Note disposing has been done. _disposed = true; // If disposing equals true, dispose all managed // and unmanaged resources. if (disposing) { if (_buffers != null) { for (int n = 0; n < _buffers.Length; n++) { if (_buffers[n] != null) { _buffers[n].Dispose(); } } } lock (_waveOutLock) { WaveInterop.waveOutClose(_hWaveOut); } if (_audioReader != null) { _audioReader.Dispose(); } // If the stream was created internally. if (_internalStream) { if (_audioStream != null) { _audioStream.Dispose(); } } // Close the callback. _callbackInfo.Disconnect(); } // Call the appropriate methods to clean up // unmanaged resources here. // If the stream was created internally. _buffers = null; _audioReader = null; // If the stream was created internally. if (_internalStream) { _audioStream = null; } } }