CoreAudioApi与线程 - InvalidCastException的 [英] CoreAudioApi with threading - InvalidCastException

查看:1277
本文介绍了CoreAudioApi与线程 - InvalidCastException的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个应用程序修改其他应用程序的体积,使用 CoreAudioApi DLL来做到这一点。的code以下块完美地工作在主线程,但是当在一个单独的线程调用,将 InvalidCastException的上升。我扔在一个互斥体的情况下,这是两个线程试图访问同一资源的只是一个问题,但不会出现这种情况。任何想法是什么问题呢?我难倒作为一个新的C#程序员。我读了其他问题,其中的建议,只有主线程可以访问COM对象,如CoreAudioApi,所以我需要发送一些消息给主线程。如果这是正确的,什么是做到这一点的最好方法是什么?

例外(发生在第一Math.abs if语句):

无法转换类型的COM对象系统.__ ComObject接口类型CoreAudioApi.Interfaces.IMMDevice。此操作失败的原因是对IID为{D666063F-1587-4E43-81F1-B948E807363F}'下面的错误失败,因为该接口的COM组件调用QueryInterface:不支持此接口(异常来自HRESULT:0x80004002(E_NOINTERFACE))

code:

  muteSoundMutex.WaitOne();
AudioSessionControl SASC = sInfo.getSAudioSession();
如果(Math.Abs​​(sASC.SimpleAudioVolume.MasterVolume  - 
      (0.05f / defaultDevice.AudioEndpointVolume.MasterVolumeLevelScalar))≤; 0.0001
    &功放;&安培; savedVol> 0)
{
   ... //其他code在这里。所有互斥正确释放。
 

解决方案

您的错误消息告诉一些操作在铸造过程中误差 IMMDevice
据<一href="http://daisy-trac.cvsdude.com/urakawa-sdk/browser/trunk/csharp/audio/NAudio/CoreAudioApi/Interfaces/IMMDevice.cs?rev=2379"相对=nofollow>这个规范, IMMDevice 只有4种方法:

  • 启用
  • OpenPropertyStore
  • GETID
  • GETSTATE

您所提供的code没有提及这些方法,所以要么你误会是在哪里发生的错误,或者这是内部错误上升到您的应用程序。

<打击>我想这是从你的设备获取卷过程中发生内部错误。

如果出现此错误进 sASC.SimpleAudioVolume.MasterVolume defaultDevice.AudioEndpointVolume.MasterVolumeLevelScalar ,那么这个错误因为$的C $ C上为背景模式。否则,请添加一些code和堆栈跟踪你的例外。

,错误code的误差 E_NOINTERFACE 期间可能出现的 启用 方法的设备。因此,你需要调用这个方法到你的主线程激活的设备,之后尝试使用它变成你的背景模式。不能提供任何样品code,不好意思。

I'm writing an application to modify the volume of another application and using the CoreAudioApi dll to do so. The following block of code works perfectly on the main thread, but when called in a separate thread, an InvalidCastException is raised. I threw in a Mutex in case this was just an issue of two threads trying to access the same resource, but that doesn't appear to be the case. Any ideas as to what the problem might be? I'm stumped as a new C# programmer. I read other questions where its suggested that only the main thread has access to COM objects such as CoreAudioApi, so I need to send some kind of message to the main thread. If this is correct, what is the best way to do this?

Exception (occurs on the first Math.abs if statement):

Unable to cast COM object of type 'System.__ComObject' to interface type 'CoreAudioApi.Interfaces.IMMDevice'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{D666063F-1587-4E43-81F1-B948E807363F}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

Code:

muteSoundMutex.WaitOne();
AudioSessionControl sASC = sInfo.getSAudioSession();
if (Math.Abs(sASC.SimpleAudioVolume.MasterVolume -
      (0.05f / defaultDevice.AudioEndpointVolume.MasterVolumeLevelScalar)) < 0.0001 
    && savedVol > 0)
{
   ... // other code here. all mutexes are released correctly.

解决方案

Your error message tells about casting error during some operation with IMMDevice.
According this spec, IMMDevice has only 4 methods:

  • Activate
  • OpenPropertyStore
  • GetId
  • GetState

The code you've provided has no mention of these methods, so either you've misunderstand where is your error occurs, or this is internal error raised up to your application.

I think this is an internal error during getting the Volume from your devices.

If this error occurs into the sASC.SimpleAudioVolume.MasterVolume or defaultDevice.AudioEndpointVolume.MasterVolumeLevelScalar, then this error because of code is running into background mode. Otherwise, please add some code and stacktrace for your exception.

Error with error code E_NOINTERFACE can occur during Activate method for the device. So you need call this method into your main thread to activate device, and after that try to use it into your background mode. Can't provide any sample code, sorry.

这篇关于CoreAudioApi与线程 - InvalidCastException的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆