Capture audio with AMR-NB encoding in UWP?

0

On the supported codecs page, AMR-NB is listed as one of the few encodings that is supported for both encode and decode on a variety of platforms. However, it is conspicuously absent from any of the AudioEncodingProperties "Create" methods, such as AudioEncodingProperties.CreateMp3 but no corresponding AudioEncodingProperties.CreateAmr method. All attempts to create an AMR encoder by hand have failed with the classic ambiguous UWP HRESULT-based exception.

For example,

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

results in System.Exception: 'The requested attribute was not found. (Exception from HRESULT: 0xC00D36E6)'

c#
audio
uwp
codec
amr
asked on Stack Overflow Oct 27, 2020 by Jeff

1 Answer

0

Had to add this line to set the "auto preset mode":

recordProfile.Audio.Properties.Add(new Guid("23e5cad8-a3fc-4f0f-97c3-dff51d03bc92"), 1.ToString());
answered on Stack Overflow Oct 27, 2020 by Jeff

User contributions licensed under CC BY-SA 3.0