如何仅在 Windows Phone 8 应用程序中通过听筒播放音频

问题描述:

我尝试过使用 AudioRoutingManager 类...但我遇到了未经授权的访问异常.这是我的代码

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

Windows.Phone.Media.Devices 命名空间中的 API 需要 ID_CAP_AUDIOROUTING 和 ID_CAP_VOIP 功能.(将此添加到您的清单中)

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)

此外,只能在正在进行的 VOIP 通话中更改音频路由.

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

此外,您需要在后台 VOIP 进程中进行音频路由,而不是在前台进程中.

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