Recording audio content

Updated: May 06, 2022

You can record audio content by attaching an audio capture device (microphone) as the input and a file (instead of a device) as the output.

Note: To record audio, you must enable the MMF audio recorder routing plugin in the configuration file, by adding or uncommenting the following lines:
To ensure the plugin is loaded, this configuration must be defined when you launch mm-renderer.

The following example shows how to give mm-renderer an output URL of type file: to target a file, and an input URL of type snd: to select and configure a microphone, and then start and stop recording captured audio content to the targeted file. An snd: input URL targeting a microphone works only with the file output type, so your code must obey this design.

After connecting to mm-renderer and creating a context, we attach a file as the output:
/* Code to connect to mm-renderer and create a context goes here 
   (see "Playing audio content" for an example of doing this) */
const mmr_error_info_t* errorInfo;

// Specify a file output so the audio content is recorded to a file
const char* outputPath = "file:/tmp/testFile.amr";
int outputID = mmr_output_attach( ctxt_audiorec, outputPath, "file" );

if (outputID == -1) {
    errorInfo = mmr_error_info(ctxt_audiorec);
    /* Remaining error-handling code goes here */
    return EXIT_FAILURE;
Note that the file: prefix for the output URL is optional; we just use it in this example to make it clear that the particular output is directed to a file. Also, our URL names a file with an .amr extension, which corresponds to an Adaptive Multi-Rate (AMR) file container, but mm-renderer supports many file containers and extensions:
You can record audio content for as long as you like, but you must ensure that the output file can hold all of the content you want to capture. The size of the generated output depends on many settings, including the sampling rate and number of channels, which can be set in the input URL. To attach a microphone as the input, we must provide an snd: input URL type:
// Specify which audio device under /dev/snd you want to use for
// recording and the recording details, in the input URL
int inputID = mmr_input_attach( ctxt_audiorec,
                                "track" );

if (inputID == -1) {
    /* Error-handling code goes here */
    return EXIT_FAILURE;
Our program records in mono by specifying one channel (nchan=1) in the URL, and specifies a sampling rate of 8000 Hz (frate=8000). On your platform, your microphone might allow you to record in stereo, so you could specify two channels (nchan=2). You could also increase the sampling rate to attain the necessary audio quality; for example, you could use the standard CD sampling rate of 44.1 kHz (frate=44100). The full set of options that you can configure in the URL is:

With the output and input attached, we can define output parameters; there are no input parameters for audio capture devices. For file outputs, mm-renderer supports the audio_fourcc and audio_bitrate parameters, to select an encoder and enable compression. For more details, see Parameters that affect how the output is delivered.

After all parameters have been defined, we use the same function as for playback to start the media flow, wait for the appropriate amount of time (which in a real application, would likely be based on user activity rather than a pre-set interval), then call the usual API function to stop the media flow:
if ( mmr_play(ctxt_audiorec) < 0 ) {
    /* Error-handling code goes here */
sleep(30); // Delay for the length of time you want to record

if ( mmr_stop(ctxt_audiorec) < 0 ) {
    /* Error-handling code goes here */