Как устранить ошибку создания AudioGraph в приложении UWP при попытке записи? - PullRequest
0 голосов
/ 01 мая 2019

Я пытаюсь записать звук с помощью приложения UWP и подключил кнопку «Запись» на интерфейсе к следующему:

 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.Хотя я понимаю, откуда в моем коде возникает эта ошибка, у меня возникают проблемы с определением причины возникновения ошибки.Я думаю, что это связано с форматом записи, но нуждаюсь в дополнительном руководстве.

...