Is there a way to get notified whenever a headphone jack is unplugged in a WinRT app? I want to be able to pause MediaElement playback when it happens to prevent the sound from leaking out through loudspeakers.
Jack detection notification is exposed through IMMNotificationClient::OnDeviceStateChanged. Just tried it on win8 desktop, OnDeviceStateChanged gets called with DEVICE_STATE_UNPLUGGED and DEVICE_STATE_ACTIVE when (un)plugging a device. Sadly that part of wasapi is documented as desktop only so i guess there's no way to do it in WinRT.
Try the following:
Windows::Devices::Enumeration::DeviceInformation::CreateWatcher(Windows::Devices::Enumeration::DeviceClass::AudioRender);
This works on WinRT.
See MSDN for more detail on the method: http://msdn.microsoft.com/en-us/library/ie/windows.devices.enumeration.deviceinformation.createwatcher
The only way it should be possible is through WASAPI - Windows Audio Session API (see WASAPI sample).
Sadly, WASAPI isn't exposed in c#, only in C++ (but I'm not sure if it covers jack detection or not. I honestly don't believe that they expose jack detection at the API level...).
Hopes this help.
来源:https://stackoverflow.com/questions/16538007/how-to-detect-unplugged-headphone-jack-in-winrt