尝试记录时如何解决UWP App中的AudioGraph创建错误?

时间:2019-05-01 04:48:47

标签: c# uwp audio-recording

我正在尝试使用UWP应用程序录制音频,并将UI上的“录制”按钮连接到以下位置:

 public DelegateCommand PlayCommand { get; private set; }
 public DelegateCommand StopCommand { get; private set; }
 public DelegateCommand RecordCommand { get; private set; }
 public DelegateCommand PauseCommand { get; private set; }

 private AudioGraph audioGraph;
 private DeviceInformation selectedDevice;
 private TimeSpan duration;
 private TimeSpan position;
 private AudioFileOutputNode fileOutputNode;
 private AudioFileInputNode fileInputNode;
 private AudioDeviceOutputNode deviceOutputNode;
 private AudioDeviceInputNode deviceInputNode;

 private  MainViewModel()
    {
        timer = new DispatcherTimer
        {
            Interval = TimeSpan.FromMilliseconds(500)
        };
        timer.Start();
        timer.Tick += TimeOnTick;

        PlayCommand = new DelegateCommand(Play);
        StopCommand = new DelegateCommand(Stop);
        RecordCommand = new DelegateCommand(Record);
        PauseCommand = new DelegateCommand(Pause);
        Devices = new ObservableCollection<DeviceInformation>();
        Volume = 100;
        PlaybackSpeed = 100;
        recordingFormat = MediaEncodingProfile.CreateWav(AudioEncodingQuality.Low);
        recordingFormat.Audio = AudioEncodingProperties.CreatePcm(16000,1,16);  
        EnableCommands(false);
        player = new MediaPlayerElement();
    }

 private async void Record()
    {
        if (audioGraph == null)
        {
            var settings = new AudioGraphSettings(AudioRenderCategory.Media);
            settings.EncodingProperties = recordingFormat.Audio;
            var res = await AudioGraph.CreateAsync(settings);
            if (res.Status != AudioGraphCreationStatus.Success)
            {
                Diagnostics += $"Audio Graph Creation Error: {res.Status}\r\n";
                return;
            }

            audioGraph = res.Graph;
            audioGraph.UnrecoverableErrorOccurred += OnAudioGraphError;
        }

        if (deviceInputNode == null)
        {
            var res = await audioGraph.CreateDeviceInputNodeAsync(MediaCategory.Speech,
                recordingFormat.Audio, SelectedInputDevice);
            // if you get AudioDeviceNodeCreationStatus.AccessDenied -
            //remember to add microphone capabilities
            if (res.Status != AudioDeviceNodeCreationStatus.Success)
            {
                Diagnostics += $"Device Input Node Error: {res.Status}\r\n";
                return;
            }

            deviceInputNode = res.DeviceInputNode;
        }

        var outputStorageFile = await SelectOutputFile();
        if (outputStorageFile != null)
        {
            var res = await audioGraph.CreateFileOutputNodeAsync(
                outputStorageFile, recordingFormat);
            if (res.Status != AudioFileNodeCreationStatus.Success)
            {
                Diagnostics += $"Output File Error: {res.Status}\r\n";
                return;
            }

            fileOutputNode = res.FileOutputNode;

            // construct the graph
            deviceInputNode.AddOutgoingConnection(fileOutputNode);
        }

        audioGraph.Start();
        EnableCommands(true);
        Diagnostics += "Started recording\r\n";
    }

当我通过Visual Studio 2017运行应用程序时,收到错误Audio Graph Creation Error: UnknownFailure。虽然我知道该错误在代码中的起源,但是我在确定为什么引发该错误方面遇到了挑战。我认为这与recordFormat有关,但需要进一步的指导。

0 个答案:

没有答案