How to play audio through headphones only in the Windows Phone 8 application

I tried with the AudioRoutingManager class ... but I got an 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

+5
source share
2 answers

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

In addition, you can only change the sound routing with an active VOIP call.

In addition, you need to route audio in the background VOIP process, not in the foreground process.

+8
source

Old question, but now I know the answer.

Two things you need to do: 1. Mark the corresponding sound as “link”

, , API . . IAudioClient2:: SetClientProperties AudioClientProperties, AudioClientProperties.eCategory = AudioCategory_Communications.

  1. " IP" " " WindowsPhoneReservedAppInfo.xml :

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

:

Windows Phone 8.1

+2

All Articles