音频录制器示例
发现可用设备和支持的编解码器。
音频录制器演示了如何识别可用设备和支持的编解码器,以及 QAudioRecorder 类的使用。
运行示例
要从 Qt Creator,打开Welcome 模式,并从Examples 中选择示例。更多信息,请参阅Qt Creator: 教程:构建并运行。
显示窗口和音频设置
我们会显示一个窗口,供用户选择适当的音频输入、编解码器、容器、采样率和通道。它允许设置质量或比特率。最后,可以选择输出文件并开始录音。
使用以下方法填充列表:
- QMediaDevices::audioInputs()
- QMediaFormat::supportedAudioCodecs
- QMediaFormat::supportedFileFormats
- QAudioDevice::maximumSampleRate()
- QAudioDevice::minimumSampleRate()
质量滑块的设置范围从 0(零)到QMediaRecorder::VeryHighQuality ,默认值为QMediaRecorder::NormalQuality ,而比特率框则被硬编码到列表中。
录制音频
要录制音频,我们只需创建一个 QAudioRecorder 对象、
audioRecorder = new QAudioRecorder(this);
并如上所述设置列表。录音和暂停按钮上的文字会根据audioRecorder
对象的state 进行切换。这意味着,如果状态是QMediaRecorder::StoppedState ,按钮文本将是 "录制 "和 "暂停"。在QMediaRecorder::RecordingState 中,录制按钮的文字是 "停止",而在QMediaRecorder::PausedState 中,暂停按钮的文字是 "继续"。
按下按钮还将根据状态进行切换。如果录制已停止,那么按下录制按钮将在audioRecorder
对象上设置编码设置和容器,并使用record() 方法开始录制。
QMediaFormat format; format.setCodec(boxValue(ui->audioCodecBox).toString()); audioRecorder->setMediaFormat(format); audioRecorder->setSampleRate(boxValue(ui->sampleRateBox).toInt()); audioRecorder->setBitRate(boxValue(ui->bitrateBox).toInt()); audioRecorder->setQuality(QMediaRecorder::EncodingQuality(ui->qualitySlider->value())); audioRecorder->setEncodingMode(ui->constantQualityRadioButton->isChecked() ? QMediaRecorder::ConstantQualityEncoding : QMediaRecorder::ConstantBitRateEncoding); QString container = boxValue(ui->containerBox).toString(); audioRecorder->record();
在录制过程中,应用程序的状态栏会根据来自audioRecorder
对象的durationChanged 信号的持续时间信息进行更新。
ui->statusbar->showMessage(tr("Recorded %1 sec").arg(duration / 1000));
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.