如何使用默认输出设备作为使用AudioGraph录制音频的源?

时间:2018-01-26 15:39:58

标签: c# audio uwp

在我的UWP应用程序中,我有以下代码,它与输入设备(DeviceInformation)配合使用,可以录制音频并进行处理。我想通过使用默认输出设备而不是麦克风来扩展它。这基本上意味着应用程序将分析音频卡和扬声器的音频。

这是我的代码:

 AudioGraphSettings settings = new AudioGraphSettings(AudioRenderCategory.Media)
            {
                QuantumSizeSelectionMode = QuantumSizeSelectionMode.LowestLatency
            };

            CreateAudioGraphResult result = await AudioGraph.CreateAsync(settings);

            if (result.Status != AudioGraphCreationStatus.Success)
            {
                // Cannot create graph
                System.Diagnostics.Debug.WriteLine(String.Format("AudioGraph Creation Error because {0}", result.Status.ToString()));
                return;
            }

            graph = result.Graph;

            // Create a device output node
            CreateAudioDeviceOutputNodeResult deviceOutputNodeResult = await graph.CreateDeviceOutputNodeAsync();
            if (deviceOutputNodeResult.Status != AudioDeviceNodeCreationStatus.Success)
            {
                return;
            }

            AudioDeviceOutputNode deviceOutputNode = deviceOutputNodeResult.DeviceOutputNode;
            System.Diagnostics.Debug.WriteLine("Device Output connection successfully created");


            // Create a device input node using the default audio input device
            CreateAudioDeviceInputNodeResult deviceInputNodeResult = await graph.CreateDeviceInputNodeAsync(MediaCategory.Other, graph.EncodingProperties, SelectedDevice);

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

                return;
            }

            AudioDeviceInputNode deviceInputNode = deviceInputNodeResult.DeviceInputNode;
            System.Diagnostics.Debug.WriteLine("Device Input connection successfully created");


            frameOutputNode = graph.CreateFrameOutputNode();
            deviceInputNode.AddOutgoingConnection(frameOutputNode);

            AudioFrameInputNode frameInputNode = graph.CreateFrameInputNode();
            frameInputNode.AddOutgoingConnection(deviceOutputNode);
            //   frameInputNode.QuantumStarted += FrameInputNode_QuantumStarted;

            // Attach to QuantumStarted event in order to receive synchronous updates from audio graph (to capture incoming audio).
            graph.QuantumStarted += GraphOnQuantumProcessed;

如何在

中使用默认的输出设备
CreateAudioDeviceInputNodeResult deviceInputNodeResult = await graph.CreateDeviceInputNodeAsync(MediaCategory.Other, graph.EncodingProperties, SelectedDevice);

1 个答案:

答案 0 :(得分:0)

要在一行中获取当前的默认音频输出(渲染)设备:

DeviceInformation defaultDevice = await DeviceInformation.CreateFromIdAsync(MediaDevice.GetDefaultAudioRenderId(AudioDeviceRole.Default));

您还可以使用MediaDevice类来获取默认的Capture设备并监听每个事件的已更改事件。

似乎您不能使用“渲染”设备作为输入节点来录制系统音频(see here