我创建了一个类,它派生自System.Windows.Media.AudioSink
以提供录制功能。要检查我的混凝土水槽的状态,我会执行以下操作:
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;
}
}
}
有时当我查询proprety IsRecording
时,我会得到以下异常:
{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()
有时当我this._myAudioSink.Stop()
时,我会得到以下(类似)异常:
{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()
那么......原因是什么?如何防止此异常(除了引入我自己的字段并将其设置为OnCaptureStarted
和OnCaptureStopped
的覆盖之外)?
我在网上找到的关于这个问题的唯一话题是here ...
答案 0 :(得分:0)
我最终覆盖了特定的命令(OnCaptureStopped
,OnCaptureStarted
,..)以封装我的模型中的状态 AND 封装了我的具体音频中的captureSource-沉入一个单独的领域。
显然这就是诀窍,我再也没有遇到任何问题。
PS:我在某处读到了captureSource的封装应该足够了 - 我无法提供链接......它就在那里,sry!