使用UWP应用程序中的AudioGraph将音频输出发送到两个设备

时间:2017-02-08 19:06:25

标签: c# audio uwp

我尝试将麦克风的输入同时发送到扬声器和连接的耳机。 我知道如何使用文件和设备执行此操作但我不知道如何将第二个设备与输出节点连接并将其附加到图表。 我可以使用CreateDeviceOutputNodeAsync创建节点,但我没有线索将设备与节点相关联。 对于一个设备,当我在创建图形时在设置中将设备作为PrimaryRenderDevice提供时,它可以工作。 这是一个输出设备的工作代码。我不知道如何解决这个问题,使其适用于两个输出设备。

private async Task CreateAudioGraph()
{
       AudioGraphSettings settings = new AudioGraphSettings(AudioRenderCategory.Speech);
        settings.QuantumSizeSelectionMode = QuantumSizeSelectionMode.LowestLatency;
        settings.DesiredRenderDeviceAudioProcessing = Windows.Media.AudioProcessing.Raw; // this is also set automatically when LowestLatency is set
        settings.PrimaryRenderDevice = OutputDeviceForInput;

        CreateAudioGraphResult result = await AudioGraph.CreateAsync(settings);

        if (result.Status != AudioGraphCreationStatus.Success)
        {
            // Cannot create graph

            ConnectionStatus = String.Format("AudioGraph Creation Error because {0}", result.Status.ToString());
            return;
        }

        graphForInputDeviceNo1 = result.Graph;
        ConnectionStatus = "Graph successfully created!";


        // Create a device input node using the selectedt input device
        CreateAudioDeviceInputNodeResult deviceInputNodeResult = await graphForInputDeviceNo1.CreateDeviceInputNodeAsync(MediaCategory.Speech, graphForInputDeviceNo1.EncodingProperties, FirstInputDevice);

        if (deviceInputNodeResult.Status != AudioDeviceNodeCreationStatus.Success)
        {
            // Cannot create device input node
            ConnectionStatus = String.Format("Audio Device Input unavailable because {0}", deviceInputNodeResult.Status.ToString());
            return;
        }

        deviceInputNode = deviceInputNodeResult.DeviceInputNode;

        // Create the first device output node
        CreateAudioDeviceOutputNodeResult deviceOutputNodeNo1Result = await graphForInputDeviceNo1.CreateDeviceOutputNodeAsync();
        if (deviceOutputNodeNo1Result.Status != AudioDeviceNodeCreationStatus.Success)
        {
            // Cannot create device output node
            ConnectionStatus = String.Format("Audio Device Output unavailable because {0}", deviceOutputNodeNo1Result.Status.ToString());
            return;
        }

        deviceOutputNodeNo1 = deviceOutputNodeNo1Result.DeviceOutputNode;

        // Create the second device output node
        CreateAudioDeviceOutputNodeResult deviceOutputNodeNo2Result = await graphForInputDeviceNo1.CreateDeviceOutputNodeAsync();
        if (deviceOutputNodeNo2Result.Status != AudioDeviceNodeCreationStatus.Success)
        {
            // Cannot create device output node
            ConnectionStatus = String.Format("Audio Device Output unavailable because {0}", deviceOutputNodeNo2Result.Status.ToString());
            return;
        }
        deviceOutputNodeNo2 = deviceOutputNodeNo2Result.DeviceOutputNode;

        ConnectionStatus = "Device Output connection successfully created";


        deviceInputNode.AddOutgoingConnection(deviceOutputNodeNo1);

        ConnectionStatus = "Device Input connection successfully created";
        deviceInputNode.AddOutgoingConnection(deviceOutputNodeNo2);

        // Because we are using lowest latency setting, we need to handle device disconnection errors
        graphForInputDeviceNo1.UnrecoverableErrorOccurred += Graph_UnrecoverableErrorOccurred;
        graphForInputDeviceNo1.Start();  // erst damit geht es los
}

0 个答案:

没有答案