检测何时拔下麦克风 [英] Detecting when a microphone is unplugged

查看:34
本文介绍了检测何时拔下麦克风的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Windows 窗体应用程序,其中有一个 Start 和一个 Stop 按钮.当我点击 Start 按钮时,录制应该开始,当我点击 Stop 按钮时,录制应该停止.它工作正常,但我的要求是当我开始录音时,如果我拔掉麦克风,录音应该停止.但是,我当前的代码没有这样做.这是我当前的代码:

uint hr = MFRecWrapper.StartRecording(microPhoneName.Trim(), this.fileName.Trim(), this.bitSamplingBitrate, this.avgBytesWrittenPerSecond, this.Handle);uint hr = MFRecWrapper.StopRecording();

我该如何制作才能达到这种效果?谢谢.

解决方案

我猜你将不得不通过非托管代码来完成它并挂钩 Windows 事件.WM_DEVICECHANGE

示例

 使用 System.Runtime.InteropServices;const int WM_DEVICECHANGE = 0x0219;//插入新设备const int DBT_DEVICEARRIVAL = 0x8000;//设备被移除const int DBT_DEVICEREMOVECOMPLETE = 0x8004;//设备被改变const int DBT_DEVNODES_CHANGED = 0x0007;protected override void WndProc(ref Message m){if (m.Msg == WM_DEVICECHANGE{//你的代码在这里.}base.WndProc(ref m);}

I have a Windows Forms application and in this I have a Start and a Stop button. When I click the Start button a recording should start and when I click the Stop button the recording should stop. It's working fine, but my requirement is when I started recording, if I unplug the microphone the recording should stop. However, my current code does not do this. Here is my current code:

uint hr = MFRecWrapper.StartRecording(microPhoneName.Trim(), this.fileName.Trim(), this.bitSamplingBitrate, this.avgBytesWrittenPerSecond, this.Handle);
uint hr = MFRecWrapper.StopRecording();

How can I make it so that it achieves that effect? Thanks.

解决方案

I guess you will have to do it through unmanaged code and hook to windows event. WM_DEVICECHANGE

Example

   using System.Runtime.InteropServices;
    const int WM_DEVICECHANGE = 0x0219;
     // new device is pluggedin
     const int DBT_DEVICEARRIVAL = 0x8000; 
     //device is removed 
    const int DBT_DEVICEREMOVECOMPLETE = 0x8004; 
     //device is changed
    const int DBT_DEVNODES_CHANGED = 0x0007; 
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_DEVICECHANGE
         {
              //Your code here.
         }
       base.WndProc(ref m);
    }

这篇关于检测何时拔下麦克风的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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