how to play audio through earpiece only in windows

2019-01-25 17:29发布

问题:

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

回答1:

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.



回答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