protected override void PlayAndRecord(string WavFileName, Channel Channel) { bool IsEqual = false; ProductName = string.Empty; List <Guid> AudioDeviceList = null; SetupApi = new SetupApi(); di.Clear(); switch (Channel) { case Channel.Left: case Channel.Right: case Channel.AudioJack: AudioDeviceList = MachineAudioDeviceList; break; case Channel.HeadSet: AudioDeviceList = ExternalAudioDeviceList; break; case Channel.Fan: AudioDeviceList = FanRecordDeviceList; break; } for (int n = -1; n < WaveOut.DeviceCount; n++) { var caps = WaveOut.GetCapabilities(n); Console.WriteLine("Play device {0}: {1}", n, caps.ProductName); Console.WriteLine(caps.ManufacturerGuid); Trace.WriteLine(caps.ManufacturerGuid); foreach (var v in AudioDeviceList) { if (caps.ManufacturerGuid.Equals(v)) { DeviceNumber = n; ProductName = caps.ProductName; SetupApi.GetLocationInformation(DeviceNumber, ProductName); Console.WriteLine("Find"); } } } switch (Channel) { case Channel.Left: case Channel.Right: if (string.IsNullOrEmpty(ProductName)) { throw new Exception("Machine audio device not found"); } break; case Channel.HeadSet: IsEqual = ExternalAudioDeviceList.Except(FanRecordDeviceList).Count() == 0; if (IsEqual) { if (SetupApi.di.Count() < 2 || ProductName.Contains(UsbAudioDeviceName)) { throw new Exception("External audio device not found"); } } else { if (string.IsNullOrEmpty(ProductName) || ProductName.Contains(UsbAudioDeviceName)) { throw new Exception("External audio device not found"); } } DeviceNumber = SetupApi.di.OrderBy(e => e.Value).FirstOrDefault().Key; //ProductName = SetupApi.di.OrderBy(e => e.Value).FirstOrDefault().Value; break; case Channel.AudioJack: if (string.IsNullOrEmpty(ProductName)) { throw new Exception("Audio Jack device not found"); } break; case Channel.Fan: IsEqual = ExternalAudioDeviceList.Except(FanRecordDeviceList).Count() == 0; if (IsEqual) { if (SetupApi.di.Count() < 2 || ProductName.Contains(UsbAudioDeviceName)) { throw new Exception("Fan record device not found"); } } else { if (string.IsNullOrEmpty(ProductName) || ProductName.Contains(UsbAudioDeviceName)) { throw new Exception("Fan record device not found"); } } DeviceNumber = SetupApi.di.OrderBy(e => e.Value).LastOrDefault().Key; //ProductName = SetupApi.di.OrderBy(e => e.Value).LastOrDefault().Value; break; } using (var inputReader = new WaveFileReader(WavFileName)) using (var outputDevice = new WaveOutEvent()) { outputDevice.DeviceNumber = DeviceNumber; Console.WriteLine("Play device: ###### {0} ######", WaveOut.GetCapabilities(DeviceNumber).ProductName); foreach (var item in SetupApi.di.OrderBy(e => e.Value)) { Console.WriteLine(item.Key + " " + item.Value); } outputDevice.Init(inputReader); outputDevice.PlaybackStopped += (sender, e) => { if (WavSource != null) { WavSource.StopRecording(); } Console.WriteLine("Play Stopped"); }; outputDevice.Play(); switch (Channel) { case Channel.Left: Record(Channel.Left).Wait(); break; case Channel.Right: Record(Channel.Right).Wait(); break; case Channel.AudioJack: Record(Channel.AudioJack).Wait(); break; case Channel.HeadSet: Record(Channel.HeadSet).Wait(); break; case Channel.Fan: Record(Channel.Fan).Wait(); break; } while (outputDevice.PlaybackState == PlaybackState.Playing) { Thread.Sleep(500); } } }
protected override void PlayAndRecord(string WavFileName, Channel Channel) { ProductName = string.Empty; List <Guid> AudioDeviceList = null; if (Channel == Channel.Left || Channel == Channel.Right) { AudioDeviceList = MachineAudioDeviceList; } else if (Channel == Channel.HeadSet) { AudioDeviceList = ExternalAudioDeviceList; } for (int n = -1; n < WaveOut.DeviceCount; n++) { var caps = WaveOut.GetCapabilities(n); Console.WriteLine("Play device {0}: {1}", n, caps.ProductName); Console.WriteLine(caps.ManufacturerGuid); Trace.WriteLine(caps.ManufacturerGuid); foreach (var v in AudioDeviceList) { if (caps.ManufacturerGuid.Equals(v)) { DeviceNumber = n; ProductName = caps.ProductName; Console.WriteLine("Find"); } } } if (string.IsNullOrEmpty(ProductName) && (Channel == Channel.Left || Channel == Channel.Right)) { throw new Exception("Machine audio device not found"); } else if (string.IsNullOrEmpty(ProductName) && Channel == Channel.HeadSet) { throw new Exception("External audio device not found"); } using (var inputReader = new WaveFileReader(WavFileName)) using (var outputDevice = new WaveOutEvent()) { outputDevice.DeviceNumber = DeviceNumber; Console.WriteLine("Play device: {0}", ProductName); outputDevice.Init(inputReader); outputDevice.PlaybackStopped += (sender, e) => { if (WavSource != null) { WavSource.StopRecording(); } Console.WriteLine("Play Stopped"); }; outputDevice.Play(); if (Channel == Channel.Left) { Record(Channel.Left).Wait(); } else if (Channel == Channel.Right) { Record(Channel.Right).Wait(); } else if (Channel == Channel.HeadSet) { Record(Channel.HeadSet).Wait(); } while (outputDevice.PlaybackState == PlaybackState.Playing) { Thread.Sleep(500); } } }