首页 > 解决方案 > 在 UWP 中使用 AMR-NB 编码捕获音频?

问题描述

支持的编解码器页面上,AMR-NB 被列为在各种平台上同时支持编码和解码的少数编码之一。但是,任何AudioEncodingProperties“创建”方法中都明显缺少它,​​例如AudioEncodingProperties.CreateMp3但没有相应的AudioEncodingProperties.CreateAmr方法。所有手动创建 AMR 编码器的尝试都因经典的基于 UWP HRESULT 的模棱两可的异常而失败。

例如,

var capture = new MediaCapture();

await capture.InitializeAsync(new MediaCaptureInitializationSettings
{
    MediaCategory = MediaCategory.Speech,
        StreamingCaptureMode = StreamingCaptureMode.Audio
});

var recordProfile = new MediaEncodingProfile
{
    Audio = new AudioEncodingProperties
    {
        BitsPerSample = 16,
        ChannelCount = 1,
        SampleRate = 8000,
        Subtype = "AMRNB"
    }
};

ContainerEncodingProperties containerProperties = new ContainerEncodingProperties
{
    Subtype = "AMR" // tried this with every known container
};

recordProfile.Container = containerProperties;
recordProfile.Video = null;

var file = await KnownFolders.VideosLibrary.CreateFileAsync("captured.amr",
    CreationCollisionOption.ReplaceExisting);
await capture.StartRecordToStorageFileAsync(recordProfile, file); // this throws

结果是System.Exception: 'The requested attribute was not found. (Exception from HRESULT: 0xC00D36E6)'

标签: c#audiouwpcodecamr

解决方案


必须添加这一行来设置“自动预设模式”:

recordProfile.Audio.Properties.Add(new Guid("23e5cad8-a3fc-4f0f-97c3-dff51d03bc92"), 1.ToString());

推荐阅读