gpt4 book ai didi

c# - UWP MediaCapture-录制WAV音频,采样率16000,16位,单声道

转载 作者:行者123 更新时间:2023-12-02 23:01:58 74 4
gpt4 key购买 nike

我想将录音包含在我的UWP应用中。
录音应保存到wav文件中,并且必须具有属性采样率16000(16位,单声道)(语音识别任务必需)。
由于CSCore和NAudio之类的库似乎不适用于UWP应用,因此我尝试使用MediaCapture API。

这是我的代码:

MediaCapture mediaCapture = new MediaCapture();

var settings = new MediaCaptureInitializationSettings
{
AudioDeviceId = device.Id,
StreamingCaptureMode = Windows.Media.Capture.StreamingCaptureMode.Audio,
};
await mediaCapture.InitializeAsync(settings);

var localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
StorageFile file = await localFolder.CreateFileAsync("audio.wav", CreationCollisionOption.GenerateUniqueName);
await mediaCapture.StartRecordToStorageFileAsync(MediaEncodingProfile.CreateWav(AudioEncodingQuality.Auto), file);

我尝试使用以下代码设置 AudioEncodingProperties,但始终会出错:
var controller = mediaCapture.AudioDeviceController;
var properties = controller.GetMediaStreamProperties(MediaStreamType.Audio) as AudioEncodingProperties;
properties.SampleRate = 16000;
properties.BitsPerSample = 16;
properties.ChannelCount = 1;

// the following line fails with System.Exception "The request is invalid in the current state."
await controller.SetMediaStreamPropertiesAsync(MediaStreamType.Audio, properties);

任何帮助都非常感谢!

最佳答案

尝试如下配置MediaEncodingProfile.Audio。

var profile = MediaEncodingProfile.CreateWav(AudioEncodingQuality.Auto);
profile.Audio = AudioEncodingProperties.CreatePcm(16000, 1, 16);

await mediaCapture.StartRecordToStorageFileAsync(profile, file);

顺便说一句,尝试更改捕获设备端的音频质量可能不会成功,因为它不支持太多格式。
var controller = mediaCapture.AudioDeviceController;
var encoding_properties_supported = controller.GetAvailableMediaStreamProperties(MediaStreamType.Audio).ToArray();

关于c# - UWP MediaCapture-录制WAV音频,采样率16000,16位,单声道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61427616/

74 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com