When playbackdevice disapears from windows playback devices (when user unplugs device from usb or audio jack port or disable it) while playling file, an COMException occrus in DirectSoundOut.StopPlayback() method on line 475: secondaryBuffer.Stop();
I don't know if it is possible to handle the exception from wpf application since it is in another thread.
I attached working sample project. just click the play button and unplug the device. if there's more than one playback device, then disable them. Exception occurs only when last device is disabled/uplugged.
You have a try..catch..finally bock in PlaybackThreadFunc, but when an exception is caught, then you call the com methods in finally block without try catch. You should at least use try..catch(ComException).
System.Runtime.InteropServices.COMException was unhandled
Message=Exception from HRESULT: 0x88780096
Source=NAudio
ErrorCode=-2005401450
StackTrace:
at NAudio.Wave.DirectSoundOut.IDirectSoundBuffer.Stop()
at NAudio.Wave.DirectSoundOut.StopPlayback() in C:\Users\Admin\Desktop\NAudio\naudio_db92aece4b2f\NAudio\Wave\WaveOutputs\DirectSoundOut.cs:line 475
at NAudio.Wave.DirectSoundOut.PlaybackThreadFunc() in C:\Users\Admin\Desktop\NAudio\naudio_db92aece4b2f\NAudio\Wave\WaveOutputs\DirectSoundOut.cs:line 442
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
Comments: I don't seem to get any COM exceptions removing DirectSound playback devices which makes this hard to test. Can you try with the very latest NAudio code and use the NAudioDemo Audio File Playback page to see what happens?
I don't know if it is possible to handle the exception from wpf application since it is in another thread.
I attached working sample project. just click the play button and unplug the device. if there's more than one playback device, then disable them. Exception occurs only when last device is disabled/uplugged.
You have a try..catch..finally bock in PlaybackThreadFunc, but when an exception is caught, then you call the com methods in finally block without try catch. You should at least use try..catch(ComException).
System.Runtime.InteropServices.COMException was unhandled
Message=Exception from HRESULT: 0x88780096
Source=NAudio
ErrorCode=-2005401450
StackTrace:
at NAudio.Wave.DirectSoundOut.IDirectSoundBuffer.Stop()
at NAudio.Wave.DirectSoundOut.StopPlayback() in C:\Users\Admin\Desktop\NAudio\naudio_db92aece4b2f\NAudio\Wave\WaveOutputs\DirectSoundOut.cs:line 475
at NAudio.Wave.DirectSoundOut.PlaybackThreadFunc() in C:\Users\Admin\Desktop\NAudio\naudio_db92aece4b2f\NAudio\Wave\WaveOutputs\DirectSoundOut.cs:line 442
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
Comments: I don't seem to get any COM exceptions removing DirectSound playback devices which makes this hard to test. Can you try with the very latest NAudio code and use the NAudioDemo Audio File Playback page to see what happens?