5

I have tried with AudioRoutingManager class...but i got unauthorizedaccess exception. here is my code

 AudioRoutingManager audioRouting = AudioRoutingManager.GetDefault();
    public AudioRoutingEndpoint ChangeAudioRoute()
    {

       var currentEndPoint= audioRouting.GetAudioEndpoint();
       switch (currentEndPoint)
       {
           case AudioRoutingEndpoint.Earpiece:
           case AudioRoutingEndpoint.Default:
               return AudioRoutingEndpoint.Speakerphone;

           case AudioRoutingEndpoint.Speakerphone:
               return AudioRoutingEndpoint.Earpiece;

               default:
               throw new OperationCanceledException();
       }
    }

    public void SetAudioRoute()
    {
        audioRouting.SetAudioEndpoint(this.ChangeAudioRoute());
    }

enter image description here

Claus Jørgensen
  • 25,882
  • 9
  • 87
  • 150
user1934329
  • 529
  • 1
  • 6
  • 18

2 Answers2

8

The APIs in the Windows.Phone.Media.Devices namespace require the ID_CAP_AUDIOROUTING and the ID_CAP_VOIP capability. (Add this to your manifest)

Also, it's only possible to change the audio routing while in a active VOIP call.

Additionally, you need to do the audio routing in your background VOIP process, and not in the foreground process.

Claus Jørgensen
  • 25,882
  • 9
  • 87
  • 150
Patrick F
  • 1,201
  • 8
  • 11
  • Unfortunately we had tried this, added it to WMAppManifest.xml: . However, the following error is thrown: Error 14 The 'Name' attribute is invalid - The value 'ID_CAP_AUDIOROUTING' is invalid according to its datatype 'String' - The Enumeration constraint failed. – Luis R. Mar 07 '13 at 23:59
  • Well that was a bit confusing. The capability for this specific manager is actually ID_CAP_VOIP. Using your code, you won't get an exception on the AudioRoutingManager. But I think you still will on SetAudioEndPoint unless a call is in progress. – Patrick F Mar 10 '13 at 22:23
  • I'm of the same view. From all MS examples, setting the endpoint is only available while a VOIP call is in progress. This would be consistent with the OS, where a call can only be changed to speaker after it is initiated. – David Gordon Mar 11 '13 at 10:49
  • 1
    Hey, just to confirm, you can only change the audio routing while in a (VOIP) call. *(I work for Microsoft/Skype on Windows Phone)*. I've updated your answer. – Claus Jørgensen Mar 13 '13 at 11:53
  • Updated with another note. The routing needs to be done in the background process. – Claus Jørgensen Mar 13 '13 at 13:07
  • Claus Jorgensen,is push service mandatory for incoming call? is there any other alternative that we can use to receive incoming call ? like i want to use pjsip library,and want to run the pjsip library in the backgroung agent and receive incoming call from that library always without push notification,is it possible? – Shofiqul Alam Jun 03 '14 at 07:21
  • What's the equivalent to these capabilities in an 8.1 Universal App? – David Hayes Sep 30 '14 at 22:59
2

Old question but now I know the answer.

Two things which you need to do:

  1. Tag the audio in question as "communications"

How to do this depends on what API you're using. It could be as simple as . Or you might have to call IAudioClient2::SetClientProperties with an AudioClientProperties structure whose AudioClientProperties.eCategory = AudioCategory_Communications.

  1. Tag your app as either a "voice over IP" app or a "voicemail" app You should add file called WindowsPhoneReservedAppInfo.xml to your project with the following contents:

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

Look for more detailed explanation here:

Playing audio to the earpiece from a Windows Phone 8.1 universal app

mirh
  • 514
  • 8
  • 14
norekhov
  • 3,915
  • 25
  • 45