/// <summary> /// Gets the playback bytes count. /// </summary> /// <param name="deviceHandle">The device handle.</param> /// <returns>The number of bytes played during this session</returns> /// <exception cref="TimeoutException">Occurs when the interop lock cannot be acquired.</exception> /// <exception cref="LegacyAudioException">Occurs when the MME interop call fails</exception> /// <exception cref="ArgumentException">Occurs when the device does not return a byte count.</exception> public static long GetPlaybackBytesCount(IntPtr deviceHandle) { if (!TryEnterWaveOperation(deviceHandle)) { return(0); } try { var time = new MmTime { Type = MmTime.TimeBytes }; var structSize = Marshal.SizeOf(time); LegacyAudioException.Try( NativeMethods.GetPlaybackPosition(deviceHandle, out time, structSize), nameof(NativeMethods.GetPlaybackPosition)); if (time.Type != MmTime.TimeBytes) { throw new ArgumentException($"{nameof(NativeMethods.GetPlaybackPosition)}: " + $"wType -> Expected {nameof(MmTime.TimeBytes)}, Received {time.Type}"); } return(time.CB); } finally { Monitor.Exit(SyncLock); } }
/// <summary> /// Gets the current position in bytes from the wave output device. /// (n.b. this is not the same thing as the position within your reader /// stream - it calls directly into waveOutGetPosition) /// </summary> /// <returns>Position in bytes</returns> public long GetPosition() { lock (WaveOutLock) { var time = new MmTime() { Type = MmTime.TIME_BYTES }; MmException.Try(WaveInterop.NativeMethods.waveOutGetPosition(DeviceHandle, out time, Marshal.SizeOf(time)), nameof(WaveInterop.NativeMethods.waveOutGetPosition)); if (time.Type != MmTime.TIME_BYTES) { throw new Exception(string.Format($"{nameof(WaveInterop.NativeMethods.waveOutGetPosition)}: wType -> Expected {0}, Received {1}", MmTime.TIME_BYTES, time.Type)); } return(time.CB); } }
/// <summary> /// Gets the playback bytes count. /// </summary> /// <param name="deviceHandle">The device handle.</param> /// <returns>The number of bytes played during this session</returns> /// <exception cref="TimeoutException">Occurs when the interop lock cannot be acquired.</exception> /// <exception cref="MmException">Occurs when the MME interop call fails</exception> /// <exception cref="ArgumentException">Occurs when the device does not return a byte count.</exception> public static long GetPlaybackBytesCount(IntPtr deviceHandle) { if (deviceHandle == IntPtr.Zero) { return(0); } var acquired = false; Monitor.TryEnter(SyncLock, LockTimeout, ref acquired); if (acquired == false) { throw new TimeoutException(TimeoutErrorMessage); } try { var time = new MmTime() { Type = MmTime.TIME_BYTES }; var structSize = Marshal.SizeOf(time); MmException.Try( NativeMethods.waveOutGetPosition(deviceHandle, out time, structSize), nameof(NativeMethods.waveOutGetPosition)); if (time.Type != MmTime.TIME_BYTES) { throw new ArgumentException($"{nameof(NativeMethods.waveOutGetPosition)}: " + $"wType -> Expected {nameof(MmTime.TIME_BYTES)}, Received {time.Type}"); } return(time.CB); } catch { throw; } finally { Monitor.Exit(SyncLock); } }
public static extern MmResult waveOutGetPosition(IntPtr hWaveOut, out MmTime mmTime, int uSize);