如何在QT C ++中录制和保存音频?

时间:2017-08-16 11:18:25

标签: c++ qt audio recording

我正在尝试使用Qt c++录制和保存音频文件,这是我的代码。

QAudioRecorder * audioRecorder = new QAudioRecorder();

if(audioRecorder->state() == audioRecorder->StoppedState)
{
    // Set recording Settings
    QAudioEncoderSettings settings;
    settings.setCodec("audio/pcm");     // Not sure what to put here
    settings.setSampleRate(16000);
    settings.setBitRate(32);
    settings.setQuality(QMultimedia::HighQuality);
    settings.setEncodingMode(QMultimedia::ConstantQualityEncoding);

    audioRecorder->setEncodingSettings(settings);

    //Set Audio Input
    audioRecorder->setAudioInput(audioRecorder->defaultAudioInput());

    // Sets Output location where to store the file
    QUrl url("hello.wav");
    audioRecorder->setOutputLocation(QUrl::fromLocalFile("hello.wav"));
    audioRecorder->setContainerFormat("mp3");

    audioRecorder->record();

    qDebug()<<audioRecorder->state();
}
else
{
    qDebug()<<"stopped";
}

但它不保存任何文件而是显示消息no service found for - "org.qt-project.qt.audiosource"。 如何保存音频文件?

1 个答案:

答案 0 :(得分:0)

您应该安装一些Qt Multimedia Backend - 检查此列表(适用于您使用的Qt版本)并选择适用于您的操作系统的后端。