gpt4 book ai didi

c# - System.InvalidOperationException: 'RegisterSessionNotification has to be called from an MTA-Thread.'

转载 作者:行者123 更新时间:2023-12-02 23:33:15 26 4
gpt4 key购买 nike

因此,我试图为峰值量创建一个可视化工具,并在使用CsCore的网站上找到了这段代码。因此,当我尝试运行它时,它引发了以下错误消息:

System.InvalidOperationException: 'RegisterSessionNotification has to be called from an MTA-Thread.'



这是我正在使用的代码
public static void getVolume() {
using(var sessionManager = GetDefaultAudioSessionManager2(DataFlow.Render))
{
using(var sessionEnumerator = sessionManager.GetSessionEnumerator())
{
foreach(var session in sessionEnumerator)
{
using(var audioMeterInformation = session.QueryInterface<AudioMeterInformation>())
{
Debug.WriteLine(audioMeterInformation.GetPeakValue());
}
}
}
}
}

private static AudioSessionManager2 GetDefaultAudioSessionManager2(DataFlow dataFlow)
{
using(var enumerator = new MMDeviceEnumerator())
{
using (var device = enumerator.GetDefaultAudioEndpoint(dataFlow, Role.Multimedia))
{
Debug.WriteLine("DefaultDevice: " + device.FriendlyName);
var sessionManager = AudioSessionManager2.FromMMDevice(device);
return sessionManager;

}
}
}

谢谢。

最佳答案

在msdn上,您可以看到以下注释:

Note Make sure that the application initializes COM with Multithreaded Apartment (MTA) model by calling CoInitializeEx(NULL, COINIT_MULTITHREADED) in a non-UI thread. If MTA is not initialized, the application does not receive session notifications from the session manager. Threads that run the user interface of an application should be initialized apartment threading model.



测试表明,您必须从mta线程调用此函数。
只需通过新线程,线程池,任务等执行该方法。
只是主ui线程以外的任何东西。
这些是Windows核心音频api的一些限制。

关于c# - System.InvalidOperationException: 'RegisterSessionNotification has to be called from an MTA-Thread.',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49374823/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com