Jack Sensing in C#

老子叫甜甜 提交于 2019-12-06 01:23:20

问题


I am writing an application that will record audio from microphone. My problem is I need to know when the user plug in the jack and remove the jack out.

I looked into Win32_SoundDevice WMI class and seems to me there is no such a property that I can check the status of the Jack.

Then I found RegisterEndpointNotificationCallback in IMMDeviceEnumerator which seems to do what I need but I have no clue how to do this in C#. Does anyone know how to use MMDeviceEnumerator methods to check for audio port status ? Any help highly appreciated.


回答1:


You might want to take a look at then open source NAudio project.

I've browsed their code and it appears they have a .Net wrapper for your IMMDeviceEnumerator interface.

As this project is under Microsoft Public License, it'll probably be usable in your project.




回答2:


Check out with following code:

var enumerator = new MMDeviceEnumerator();
var d = enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
Console.WriteLine(d.Properties[new Guid("46d16a2c-5654-41c0-911e-7860d2bce7ee")].Value.ToString());

This property return's 1 (Plugged) or 0 (Unplugged)..

You must have the NAudio library referenced..



来源:https://stackoverflow.com/questions/9580798/jack-sensing-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!