Skip to content

Commit

Permalink
added error tolerance for device switching
Browse files Browse the repository at this point in the history
  • Loading branch information
Aytackydln committed Jan 14, 2023
1 parent bb82345 commit 657db62
Showing 1 changed file with 19 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,19 +100,27 @@ private void UpdateDevice()

private void SetDevice(MMDevice mmDevice)
{
Device = mmDevice;

// Get a WaveIn from the device and start it, adding any events as requied
WaveIn = Flow == DataFlow.Render ? new WasapiLoopbackCapture(mmDevice) : new WasapiCapture(mmDevice);
WaveIn.DataAvailable += _waveInDataAvailable;
WaveIn.RecordingStopped += WaveInOnRecordingStopped;
var fallbackWaveIn = WaveIn;
try
{
// Get a WaveIn from the device and start it, adding any events as requied
WaveIn = Flow == DataFlow.Render ? new WasapiLoopbackCapture(mmDevice) : new WasapiCapture(mmDevice);
WaveIn.DataAvailable += _waveInDataAvailable;
WaveIn.RecordingStopped += WaveInOnRecordingStopped;

var _ = mmDevice.AudioMeterInformation?.MasterPeakValue; //"Activate" device
var __ = mmDevice.AudioEndpointVolume?.MasterVolumeLevel;
var _ = mmDevice.AudioMeterInformation?.MasterPeakValue; //"Activate" device
var __ = mmDevice.AudioEndpointVolume?.MasterVolumeLevel;

DeviceName = Device.FriendlyName;
DeviceChanged?.Invoke(this, EventArgs.Empty);
WaveIn.StartRecording();
Device = mmDevice;
DeviceName = Device.FriendlyName;
DeviceChanged?.Invoke(this, EventArgs.Empty);
WaveIn.StartRecording();
}
catch (Exception e)
{
WaveIn = fallbackWaveIn;
Global.logger.Error("Error while switching sound device", e);
}
}

private void WaveInOnRecordingStopped(object sender, StoppedEventArgs e)
Expand Down

0 comments on commit 657db62

Please sign in to comment.