Example #1
0
void AudioRecorder::toggleRecord()
{
    if (audioRecorder->state() == QMediaRecorder::StoppedState) {
        audioRecorder->setAudioInput(boxValue(ui->audioDeviceBox).toString());

        QAudioEncoderSettings settings;
        settings.setCodec(boxValue(ui->audioCodecBox).toString());
        settings.setSampleRate(boxValue(ui->sampleRateBox).toInt());
        settings.setBitRate(boxValue(ui->bitrateBox).toInt());
        settings.setChannelCount(boxValue(ui->channelsBox).toInt());
        settings.setQuality(QMultimedia::EncodingQuality(ui->qualitySlider->value()));
        settings.setEncodingMode(ui->constantQualityRadioButton->isChecked() ?
                                 QMultimedia::ConstantQualityEncoding :
                                 QMultimedia::ConstantBitRateEncoding);

        QString container = boxValue(ui->containerBox).toString();

        audioRecorder->setEncodingSettings(settings, QVideoEncoderSettings(), container);
        audioRecorder->record();
    }
    else {
        audioRecorder->stop();
    }
}
bool SimpleAudioRecorder::setupRecorder()
{
    //inputs
    QStringList inputs = m_recorder.audioInputs();
    if(inputs.size() == 0) return false;
    qDebug() << "inputs: " << inputs;
    m_recorder.setAudioInput("default");

    //audio codecs;
    QStringList codecs = m_recorder.supportedAudioCodecs();
    if(codecs.size() == 0) return false;
    qDebug() << "codecs: " << codecs;
    int sampleRate = 16000;
    if(codecs.contains("aac"))
    {
        m_settings.setCodec("aac");
    }
    else if(codecs.contains("amr-wb"))
    {
        m_settings.setCodec("amr-wb");
    }
    else
    {
        m_settings.setCodec(codecs.at(0));
        sampleRate = 8000;
    }
    qDebug() << "set codec: " << m_settings.codec();

    //containers
    QStringList containers = m_recorder.supportedContainers();
    if(containers.size() == 0) return false;
    qDebug() << "containers: " << containers;
    if(containers.contains("3gp"))
    {
        m_container = "3gp";
    }
    else if(containers.contains("mp4"))
    {
        m_container = "mp4";
    }
    else
    {
        m_container = containers.at(0);
    }

    //sample rate
    QList<int> sampleRates = m_recorder.supportedAudioSampleRates();
    if(sampleRates.size() == 0) return false;
    qDebug() << "samplerates: " << sampleRates;
    if(sampleRates.size() && !sampleRates.contains(sampleRate))
    {
        sampleRate = sampleRates.at(0);
    }

    m_settings.setChannelCount(2);
    m_settings.setSampleRate(sampleRate);
    m_settings.setQuality(QMultimedia::NormalQuality);
    m_settings.setBitRate(64000);
    m_settings.setEncodingMode(QMultimedia::AverageBitRateEncoding);
    m_recorder.setEncodingSettings(m_settings
                                   , QVideoEncoderSettings()
                                   , m_container);

    connect(&m_recorder, SIGNAL(durationChanged(qint64)), this,
            SLOT(onDurationChanged(qint64)));
    connect(&m_recorder, SIGNAL(statusChanged(QMediaRecorder::Status)), this,
            SLOT(onStatusChanged(QMediaRecorder::Status)));
    connect(&m_recorder, SIGNAL(error(QMediaRecorder::Error)), this,
            SLOT(onError(QMediaRecorder::Error)));

    return true;
}