听力计c#UWP

时间:2016-03-24 14:10:14

标签: c# .net win-universal-app audio-recording

我试图在UWP上为winphone创建一个callrecorder。我试图用音频图表来做这件事。我需要为输入设备(麦克风)创建节点,输出设备(扬声器)的节点并将它们提交到文件(wave / mp3)。

我收到了例外。

        AudioGraph graph;
        AudioDeviceInputNode deviceInputNode;
        AudioDeviceOutputNode deviceOutputNode;
        AudioFileOutputNode fileOutputNode;

        private async Task InitAudiographAsync()
        {

            AudioGraphSettings settings = new AudioGraphSettings(AudioRenderCategory.Speech);
            CreateAudioGraphResult result = await AudioGraph.CreateAsync(settings);
            if (result.Status == AudioGraphCreationStatus.Success)
            {
                graph = result.Graph;

                CreateAudioDeviceOutputNodeResult deviceOutputNodeResult = await graph.CreateDeviceOutputNodeAsync();

                if (deviceOutputNodeResult.Status == AudioDeviceNodeCreationStatus.Success)
                {
                    deviceOutputNode = deviceOutputNodeResult.DeviceOutputNode;

                    var microphone = await DeviceInformation.CreateFromIdAsync(
          MediaDevice.GetDefaultAudioCaptureId(AudioDeviceRole.Default));

                    var inProfile = MediaEncodingProfile.CreateWav(AudioEncodingQuality.High);

                    var deviceInputNodeResult = await graph.CreateDeviceInputNodeAsync(MediaCategory.Speech, inProfile.Audio, microphone);

                    if (deviceInputNodeResult.Status == AudioDeviceNodeCreationStatus.Success)
                    {
                        deviceInputNode = deviceInputNodeResult.DeviceInputNode;

                        FileSavePicker saveFilePicker = new FileSavePicker();
                        saveFilePicker.FileTypeChoices.Add("Windows Media Audio", new List<string>() { ".wma" });
                        saveFilePicker.FileTypeChoices.Add("MPEG Audio Layer-3", new List<string>() { ".mp3" });
                        saveFilePicker.SuggestedFileName = "New Audio Track";
                        StorageFile file = await saveFilePicker.PickSaveFileAsync();

                        // File can be null if cancel is hit in the file picker
                        if (file == null)
                        {
                            return;
                        }

                        MediaEncodingProfile mediaEncodingProfile;
                        switch (file.FileType.ToString().ToLowerInvariant())
                        {
                            case ".mp3":
                                mediaEncodingProfile = MediaEncodingProfile.CreateMp3(AudioEncodingQuality.High);
                                break;
                            case ".wav":
                                mediaEncodingProfile = MediaEncodingProfile.CreateWav(AudioEncodingQuality.High);
                                break;
                            default:
                                throw new ArgumentException();
                        }

                        CreateAudioFileOutputNodeResult fileOutputNodeResult = await graph.CreateFileOutputNodeAsync(file, mediaEncodingProfile);

                        if (fileOutputNodeResult.Status == AudioFileNodeCreationStatus.Success)
                        {
                            fileOutputNode = fileOutputNodeResult.FileOutputNode;
                            deviceInputNode.AddOutgoingConnection(deviceOutputNode);
                            deviceInputNode.AddOutgoingConnection(fileOutputNode);
                            graph.Start();
                        }

                    }
                }
            }
        }

1 个答案:

答案 0 :(得分:3)

您是否检查了用于麦克风访问和文件访问的清单\功能。您是否获得了拒绝访问权限的异常?