使用UWP以AMR-NB编码捕获音频?

问题描述

supported codecs页上,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)'

解决方法

必须添加此行以设置“自动预设模式”:

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