I’ve created a class, which derives from System.Windows.Media.AudioSink to provide recording capabilities. To check the state of my concrete sink I do the following:
public class MyViewModel
{
private readonly MyAudioSink _myAudioSink; // this field is ensured in the ctor
public bool IsRecording
{
get
{
if (this._myAudioSink == null)
{
return false; // I know that `false` is wrong ...
}
return this._myAudioSink.CaptureSource.State == CaptureState.Started;
}
}
}
Sometimes when I query against the proprety IsRecording, I get the following exception:
{System.InvalidOperationException: Capture source is not stopped
at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
at MS.Internal.XcpImports.SetValue(IManagedPeerBase obj, DependencyProperty property, DependencyObject doh)
at System.Windows.DependencyObject.SetValue(DependencyProperty property, DependencyObject doh)
at System.Windows.Media.CaptureSource..ctor()
at MS.Internal.CoreTypes.GetCoreWrapper(UInt32 typeId)
at MS.Internal.ManagedPeerTable.EnsureManagedPeer(IntPtr unmanagedPointer, Int32 typeIndex, Type type, Boolean preserveManagedObjectReference)
at MS.Internal.XcpImports.ConvertDO(IntPtr doPointer, Int32 typeIndex, Boolean releaseObjectReference)
at MS.Internal.XcpImports.ConvertType(CValue outVal, Int32 typeIndex, Boolean releaseObjectReference, Boolean deleteBuffer, IManagedPeerBase fromObject)
at MS.Internal.XcpImports.AudioSink_GetSource(AudioSink Sink)
at System.Windows.Media.AudioSink.get_CaptureSource()
at MyViewModel.get_IsRecording()
Sometimes when I do this._myAudioSink.Stop(), I get the following (similar) exception:
{System.InvalidOperationException: Capture source is not stopped
at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
at MS.Internal.XcpImports.SetValue(IManagedPeerBase obj, DependencyProperty property, DependencyObject doh)
at System.Windows.DependencyObject.SetValue(DependencyProperty property, DependencyObject doh)
at System.Windows.Media.CaptureSource..ctor()
at MS.Internal.CoreTypes.GetCoreWrapper(UInt32 typeId)
at MS.Internal.ManagedPeerTable.EnsureManagedPeer(IntPtr unmanagedPointer, Int32 typeIndex, Type type, Boolean preserveManagedObjectReference)
at MS.Internal.XcpImports.ConvertDO(IntPtr doPointer, Int32 typeIndex, Boolean releaseObjectReference)
at MS.Internal.XcpImports.ConvertType(CValue outVal, Int32 typeIndex, Boolean releaseObjectReference, Boolean deleteBuffer, IManagedPeerBase fromObject)
at MS.Internal.XcpImports.AudioSink_GetSource(AudioSink Sink)
at System.Windows.Media.AudioSink.get_CaptureSource()
So … what’s the reason for that? How can I prevent this exception (except introducing my own field and set it in the overrides of OnCaptureStarted and OnCaptureStopped)?
The only topic on this problem I’ve found on the net is here …
I ended up with overriding the specific commands (
OnCaptureStopped,OnCaptureStarted, ..) to encapsulate the state in my model AND encapsulated the captureSource inside my concrete audio-sink in a separate field.Obviously this does the trick, and I never had any problems again.
PS: I somewhere read that the encapsulation of the captureSource should be sufficient – atm I cannot provide the link … it’s somewhere out there, sry!