Пример #1
0
        /// <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); }
        }
Пример #2
0
        /// <summary>
        /// Restarts the audio device.
        /// </summary>
        /// <param name="deviceHandle">The device handle.</param>
        /// <exception cref="TimeoutException">Occurs when the interop lock cannot be acquired.</exception>
        /// <exception cref="LegacyAudioException">Occurs when the MME interop call fails.</exception>
        public static void RestartAudioDevice(IntPtr deviceHandle)
        {
            if (!TryEnterWaveOperation(deviceHandle))
            {
                return;
            }

            try
            {
                LegacyAudioException.Try(
                    NativeMethods.RestartPlayback(deviceHandle),
                    nameof(NativeMethods.RestartPlayback));
            }
            finally { Monitor.Exit(SyncLock); }
        }
Пример #3
0
        /// <summary>
        /// Writes the audio data.
        /// </summary>
        /// <param name="deviceHandle">The device handle.</param>
        /// <param name="header">The header.</param>
        /// <exception cref="TimeoutException">Occurs when the interop lock cannot be acquired.</exception>
        /// <exception cref="LegacyAudioException">Occurs when the MME interop call fails.</exception>
        public static void WriteAudioData(IntPtr deviceHandle, WaveHeader header)
        {
            if (header == null || !TryEnterWaveOperation(deviceHandle))
            {
                return;
            }

            try
            {
                LegacyAudioException.Try(
                    NativeMethods.WriteWaveAudioData(deviceHandle, header, Marshal.SizeOf(header)),
                    nameof(NativeMethods.WriteWaveAudioData));
            }
            finally { Monitor.Exit(SyncLock); }
        }