【问题标题】:How to get headphones plug event in UWP如何在 UWP 中获取耳机插入事件
【发布时间】:2017-03-08 12:06:42
【问题描述】:

我正在编写一个 UWP 应用程序,当耳机从设备(PC 或移动 UWP)插入/拔出时,我需要在其中监听耳机插入事件。

我已经尝试处理MediaDevice::DefaultAudioRenderDeviceChangedWindows::Devices::Enumeration::DeviceWatcher。 但它们都不能按预期工作。

我可以使用MediaDevice::DefaultAudioRenderDeviceChanged 处理默认设备更改事件。但对于耳机插头盒,默认设备不会改变。因此不会触发渲染设备更改事件。

Windows::Devices::Enumeration::DeviceWatcher 也无法捕获该事件。

所以请帮忙分享一下我如何在 UWP 中获取耳机插入事件的事件?非常感谢。

【问题讨论】:

    标签: windows uwp win-universal-app windows-10-universal


    【解决方案1】:

    我尝试处理 MediaDevice::DefaultAudioRenderDeviceChanged 和 Windows::Devices::Enumeration::DeviceWatcher。但它们都不能按预期工作。

    您可以使用DeviceWatcher 来处理DeviceWatcher.AddedDeviceWatcher.Removed 事件。

    MainPage.xaml.cpp:

    using namespace Windows::Devices::Enumeration;
    using namespace Windows::UI::Core;
    
    MainPage::MainPage()
    {
        InitializeComponent();
    }
    
    void DeviceWatcherCppSample::MainPage::btnClick_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
    {
        watcher=DeviceInformation::CreateWatcher(DeviceClass::AudioRender);
        handlerAddedToken=watcher->Added += ref new     Windows::Foundation::TypedEventHandler<DeviceWatcher ^, DeviceInformation ^>(this, &DeviceWatcherCppSample::MainPage::OnAdded);
    
        handlerRemovedToken= watcher->Removed += ref new Windows::Foundation::TypedEventHandler<DeviceWatcher ^, DeviceInformationUpdate ^>(this, &DeviceWatcherCppSample::MainPage::OnRemoved);
        handlerUpdatedToken= watcher->Updated += ref new Windows::Foundation::TypedEventHandler<Windows::Devices::Enumeration::DeviceWatcher ^, Windows::Devices::Enumeration::DeviceInformationUpdate ^>(this, &DeviceWatcherCppSample::MainPage::OnUpdated);
        watcher->Start();
    }
    
    
    void DeviceWatcherCppSample::MainPage::OnAdded(DeviceWatcher ^sender, DeviceInformation ^args)
    {
        this->Dispatcher->RunAsync(CoreDispatcherPriority::Normal, ref new Windows::UI::Core::DispatchedHandler([this, args]()
        {
            tbResult->Text = args->Name +"is Added";
        }));
    }
    
    
    void DeviceWatcherCppSample::MainPage::OnRemoved(DeviceWatcher ^sender, DeviceInformationUpdate ^args)
    {
        this->Dispatcher->RunAsync(CoreDispatcherPriority::Normal, ref new Windows::UI::Core::DispatchedHandler([this, args]()
        {
            tbResult->Text = args->Id + "is Removed";
        }));
    }
    
    
    void DeviceWatcherCppSample::MainPage::OnUpdated(Windows::Devices::Enumeration::DeviceWatcher ^sender, Windows::Devices::Enumeration::DeviceInformationUpdate ^args)
    {
        this->Dispatcher->RunAsync(CoreDispatcherPriority::Normal, ref new Windows::UI::Core::DispatchedHandler([this, args]()
        {
            tbResult->Text = args->Id + "is Updated";
        }));
    
    }
    

    MainPage.xaml:

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <StackPanel VerticalAlignment="Center">
            <Button Name="btnClick" Click="btnClick_Click">Click Me to Watch</Button>
            <TextBlock Name="tbResult"></TextBlock>
        </StackPanel>
    </Grid>
    

    这里是完整的演示:DeviceWatcherSample。也可以参考Official Sample的场景二。

    【讨论】:

    • 非常感谢猫王。它适用于手机!但它仍然无法在 PC 上使用,因为扬声器和耳机共享同一个设备,称为“扬声器/耳机”。
    • 可以正确检测到USB耳机。非USB耳机不能。它们只能通过DeviceClass::All 检测到,并且只有插件事件触发更新事件,这不是您要求的正确解决方案。因此,据我所知,在 destop 上没有明确的方法可以做到这一点。
    猜你喜欢
    • 2012-10-06
    • 1970-01-01
    • 2013-04-30
    • 2017-04-24
    • 2013-05-08
    • 2013-11-11
    • 2016-04-14
    • 2012-05-12
    • 2010-10-26
    相关资源
    最近更新 更多