×
Namespaces

Variants
Actions
Revision as of 15:19, 2 February 2014 by croozeus (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

How to detect the audio path (headset connection) on Windows Phone

From Nokia Developer Wiki
Jump to: navigation, search
Featured Article
02 Feb
2014

This article explains how to detect if a headphone (or other audio endpoint) is plugged into a WP8 device using the VOIP AudioRoutingManager API.

Warning.pngWarning: This implementation doesn't work on GDR1 devices.

Note.pngNote: The article applies only to Windows Phone 8 devices.

WP Metro Icon Multimedia.png
SignpostIcon XAML 40.png
WP Metro Icon WP8.png
Article Metadata
Code ExampleTested with
SDK: Windows Phone 8.0 SDK
Devices(s): Nokia Lumia 820
Compatibility
Platform(s):
Windows Phone 8
Article
Created: marcodalpino (15 Sep 2013)
Updated: vinayppatil (28 Jan 2014)
Last edited: croozeus (02 Feb 2014)

Contents

Introduction

There is no obvious API for detecting when the headphone jack is plugged or unplugged from the device, or for determining if any other type of headset is present.

This article introduces the Windows Phone 8 VOIP API's AudioRoutingManager class. While this class is provided mainly to route the audio to VOIP communications endpoints other than the phone's speaker, it also delivers AudioEndPointChanged events which can be used to determine the current audio endpoint device, and to be notified when this changes.

The article shows how to register to the events and determine whether the audio endpoint is a wired headphone or some other device.

Implementation

This section shows how to query for the current audio endpoint and also how to register for notifications when the endpoint changes.

Query for the current AudioRoutingEndpoint using:

AudioRoutingEndpoint current = AudioRoutingManager.GetDefault().GetAudioEndpoint();

To get notified when the endpoint status changes, we first need to register a handler for the event:

AudioRoutingManager.GetDefault().AudioEndpointChanged += AudioEndpointChanged_Handler;

The different endpoints can be determined from the AudioRoutingEndpoint enum:

Member Description
Default The default audio endpoint, that is, the speakerphone
Earpiece An earpiece
Speakerphone The speakerphone
Bluetooth A Bluetooth device
WiredHeadset A wired headset
WiredHeadsetSpeakerOnly A wired headset for output only; the input is received from the default microphone
BluetoothWithNoiseAndEchoCancellation A Bluetooth device with noise and echo cancellation

Using the above enum values we can appropriately handle each case.

public 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();
}
}

Source Code

Attached source file File:DetectEarphone.zip contains a AudioPathResolver.cs class with a Bindable property AudioPath. Also notice how AddNotificationHandler() and RemoveNotificationHandler() is called on HomePage's Load and UnLoad.

Summary

We can register for AudioEndPointChanged event on AudioRoutingManager and get notified whenever audio device changes.

This page was last modified on 2 February 2014, at 15:19.
310 page views in the last 30 days.
×