Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

How to record audio in Qt using GStreamer

From Wiki
Jump to: navigation, search

This article explains how to record audio captured by microphone to mp3 using GStreamer.

Article Metadata
Code ExampleTested with
Devices(s): Nokia N900
Platform(s): MeeGo, Maemo
Created: gnuton (29 Jun 2011)
Last edited: hamishwillee (11 Oct 2012)


Qt Multimedia offers a convenient API to record audio. While this fulfils most use cases, this API does not allow us to record an http stream, or and audio stream coming from video, and it does not allow us to save it in a particular format. GStreamer allows us to achieve these goals without great effort, and works in both Maemo and MeeGo.


The application which I'm going to show you in this article, is a Qt application which contains GStreamer API. Target of this application is record a stream coming from the microphone, convert it using lame to mp3 using a bitrate of 192 and finally saving it in a file. This can be achieved with GStreamer by using a pipeline like the following one:

gst-launch autoaudiosrc ! audioconvert ! lame bitrate=192 ! filesink location=./ciao.mp3

That pipeline can be seen converted to C in the snippet below. As we can see the filename for the filesink is set later in the code using "g_object_set". To make the application more useful I added a playback pipeline to play the audio file saved. The implementation is pretty simple to understand and does not require further explanation.

audioMgr::audioMgr() {
gst_init (NULL,NULL);
// Record pipeline
// autoaudiosrc ! audioconvert ! lame bitrate=192 ! filesink location=./ciao.mp3
recordBin = gst_pipeline_new("record-pipeline");
audioSrc = gst_element_factory_make("autoaudiosrc", "audio_in");
audioConvert = gst_element_factory_make("audioconvert", "audio_converter");
lameEncoder = gst_element_factory_make("lame", "lame_encoder");
fileSink = gst_element_factory_make("filesink", "file_sink");
gst_bin_add_many(GST_BIN(recordBin), audioSrc, audioConvert, lameEncoder, fileSink, NULL);
gst_element_link_many(audioSrc, audioConvert, lameEncoder, fileSink, NULL);
// Playback
playbackBin = gst_element_factory_make("playbin2", "playbin");
audioMgr::~audioMgr() {
gst_element_set_state(GST_ELEMENT(playbackBin), GST_STATE_NULL);
gst_element_set_state(GST_ELEMENT(recordBin), GST_STATE_NULL);
void audioMgr::record(const QString& filename, const int bitrate) {
qDebug() << "RECORD";
gst_element_set_state(GST_ELEMENT(playbackBin), GST_STATE_READY);
gst_element_set_state(GST_ELEMENT(recordBin), GST_STATE_NULL);
g_object_set(G_OBJECT(fileSink), "location", qPrintable(filename), NULL);
g_object_set(G_OBJECT(lameEncoder), "bitrate", bitrate, NULL);
gst_element_set_state(GST_ELEMENT(recordBin), GST_STATE_PLAYING);
void audioMgr::stop() {
qDebug() << "STOP";
gst_element_set_state(GST_ELEMENT(playbackBin), GST_STATE_READY);
gst_element_set_state(GST_ELEMENT(recordBin), GST_STATE_READY);
void audioMgr::play(const QString& uri) {
qDebug() << "PLAY";
g_object_set(G_OBJECT(playbackBin), "uri", qPrintable(uri), NULL);
gst_element_set_state(GST_ELEMENT(recordBin), GST_STATE_READY);
gst_element_set_state(GST_ELEMENT(playbackBin), GST_STATE_PLAYING);

The complete code, tested on the Nokia N900 can be download from the link on the right side of this page.


GStreamer API is a very powerful API which permits developer to achieve things which cannot be obtained by just using Qt Mobility Multimedia API. Just changing the audiosink this code can be used to record any other audio source available on the device. To know more about audio sources available you can run gst-inspect, a command line tool belonging to the GStreamer tool set.

This page was last modified on 11 October 2012, at 01:17.
104 page views in the last 30 days.