MeeGo 1.2 Harmattan Developer Documentation Develop for the Nokia N9

Audio Recorder Example

Files:

This example shows how to create a simple audio recorder using the QtMobility Multimedia API.

We can easily setup the capture when we create the objects using a QMediaRecorder initialized with a QAudioCaptureSource object.

     audiosource = new QAudioCaptureSource;
     capture = new QMediaRecorder(audiosource);

Then we set up the display of a list of available sources to use

     for(int i = 0; i < audiosource->deviceCount(); i++)
         deviceBox->addItem(audiosource->name(i));

And available codecs

     QStringList codecs = capture->supportedAudioCodecs();
     for(int i = 0; i < codecs.count(); i++)
         codecsBox->addItem(codecs.at(i));

We display a dialog for the user to select the appropriate codec and the input device to capture. Once selected we can use user interface objects like buttons to start and stop the recording and display elapsed time.

     for(int i = 0; i < audiosource->deviceCount(); i++) {
         if(deviceBox->itemText(idx).compare(audiosource->name(i)) == 0)
             audiosource->setSelectedDevice(i);
     }

Then use signals to indicate a change to the output filename, the codec and the audio source.

When the button to record is pushed the toggleRecord() slot will start or stop the recording process using the QMediaRecorder::record() and QMediaRecorder::stop() functions of the QMediaRecorder object.

 void AudioRecorder::toggleRecord()
 {
     if(!active) {
         recTime->setText("0 sec");
         currentTime = 0;
         capture->record();

         button->setText(tr("Stop"));
         active = true;
     } else {
         capture->stop();
         button->setText(tr("Record"));
         active = false;
     }
 }