Markus
Markus

Reputation: 549

Detect headphones in windows phone 8.1

For my Windows Phone 8.1 RT app I need to detect whether headphones are plugged in.

I found this questions which answers this issue for Windows Phone 8.0 and Windows Phone 8.1 Silverlight apps: Windows Phone - Audio Endpoint Device

It tried this code in the code-behind of my main view (actually copied from http://developer.nokia.com/community/wiki/How_to_detect_the_audio_path_(headset_connection)_on_Windows_Phone):

   AudioRoutingEndpoint currentAudioRoutingEndpoint = AudioRoutingManager.GetDefault().GetAudioEndpoint();
   AudioRoutingManager.GetDefault().AudioEndpointChanged += AudioEndpointChanged_Handler;

and the handler:

   private void AudioEndpointChanged_Handler(AudioRoutingManager sender, object args)
    {
        var audioEndPoint = sender.GetAudioEndpoint();
        switch (audioEndPoint)
        {
            case AudioRoutingEndpoint.Default:
                {
                    //default audio devide
                    break;
                }
            case AudioRoutingEndpoint.Earpiece:
                {
                    //Earpiece
                    break;
                }
            case AudioRoutingEndpoint.Speakerphone:
                {
                    //Speakerphone
                    break;
                }
            case AudioRoutingEndpoint.Bluetooth:
                {
                    //Bluetooth
                    break;
                }
            case AudioRoutingEndpoint.WiredHeadset:
                {
                    //WiredHeadset
                    break;
                }
            case AudioRoutingEndpoint.WiredHeadsetSpeakerOnly:
                {
                    //WiredHeadsetSpeakerOnly
                    break;
                }
            case AudioRoutingEndpoint.BluetoothWithNoiseAndEchoCancellation:
                {
                    //BluetoothWithNoiseAndEchoCancellation
                    break;
                }
            default:
                throw new ArgumentOutOfRangeException();
        }
    }

If I run this code I get this exception: System.UnauthorizedAccessException was unhandled by user code HResult=-2147024891 Message=Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

I guess this is because the needed capabilities (ID_CAP_VOIP and ID_CAP_AUDIOROUTING) are missing. Now my problem is that in my Windows Phone 8.1 RT app there is only a package.appxmanifest and no WMAppManifest.xml and it seems I can't define these capabilities anymore.

Please note that I really have no WMAppManifest.xml in my project as it would be in a Windows Phone 8.1 Silverlight project (there are in fact both available).

I would really appreciate any help!

EDIT: changed Windows Phone 8.1 xaml to Windows Phone 8.1 RT

Upvotes: 1

Views: 952

Answers (2)

Daniel Nguyen
Daniel Nguyen

Reputation: 137

in WP8.1 Runtime you can create a xml file WindowsPhoneReserveAppInfo.xml with below code:

<?xml version="1.0" encoding="utf-8" ?>
<WindowsPhoneReservedAppInfo xmlns="http://schemas.microsoft.com/phone/2013/windowsphonereservedappinfo">
  <SoftwareCapabilities>
    <SoftwareCapability Id="ID_CAP_VOIP" />
  </SoftwareCapabilities>
</WindowsPhoneReservedAppInfo>

It work fine. Good luck!

Upvotes: 1

rikkit
rikkit

Reputation: 1137

So two things:

  1. This feature is usable only during during a VOIP call, and only in the background task for the VOIP call - see the comments on this answer.
  2. These VOIP features are only available to Windows Phone 8.1 Silverlight apps, and again, only in VOIP background tasks. So no luck for WinRT 8.1 or Universal apps. The MSDN documentation is here (A list of other features not available in Phone 8.1 WinRT here)

So.. you probably can't do what you want to do on Windows Phone (unless you're building a VOIP app).

What feature are you trying to implement with this? Maybe there is an alternative.

Upvotes: 2

Related Questions