diff options
author | Vladimir Belyavsky <belyavskyv@gmail.com> | 2024-03-15 23:16:32 +0300 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2024-03-19 20:38:56 +0000 |
commit | aefb955d1538536e25b218e2f1a58d3ecdc3b932 (patch) | |
tree | afdd6e29326d4b2284196a0e776ae61992c4aac6 | |
parent | 19bcacbee2bdfb0a1ec5b093932676bbc1396868 (diff) |
Examples: update list of input devices in AudioRecorder example
Update the input device list when adding/removing an audio input
device (such as a Bluetooth microphone). Also try to restore the
previously selected device if it is still present in the new list.
Task-number: QTBUG-123023
Pick-to: 6.5
Change-Id: Icaaa208bfdb333045d93d1676b365e3c73cdf7f4
Reviewed-by: Artem Dyomin <artem.dyomin@qt.io>
(cherry picked from commit 7a06020c0acdd81afccd46af30d2c42582dc64c4)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
(cherry picked from commit 8b3e1820c4d1a57417c18739ca9cce7a9bbab527)
-rw-r--r-- | examples/multimedia/audiorecorder/audiorecorder.cpp | 28 | ||||
-rw-r--r-- | examples/multimedia/audiorecorder/audiorecorder.h | 4 |
2 files changed, 27 insertions, 5 deletions
diff --git a/examples/multimedia/audiorecorder/audiorecorder.cpp b/examples/multimedia/audiorecorder/audiorecorder.cpp index f529dd3e6..4131d5d51 100644 --- a/examples/multimedia/audiorecorder/audiorecorder.cpp +++ b/examples/multimedia/audiorecorder/audiorecorder.cpp @@ -74,11 +74,10 @@ void AudioRecorder::init() // m_probe->setSource(m_audioRecorder); // audio devices - ui->audioDeviceBox->addItem(tr("Default"), QVariant(QString())); - for (auto device : QMediaDevices::audioInputs()) { - auto name = device.description(); - ui->audioDeviceBox->addItem(name, QVariant::fromValue(device)); - } + m_mediaDevices = new QMediaDevices(this); + connect(m_mediaDevices, &QMediaDevices::audioInputsChanged, this, + &AudioRecorder::updateDevices); + updateDevices(); // audio codecs and container formats updateFormats(); @@ -194,6 +193,25 @@ void AudioRecorder::displayErrorMessage() ui->statusbar->showMessage(m_audioRecorder->errorString()); } +void AudioRecorder::updateDevices() +{ + const auto currentDevice = boxValue(ui->audioDeviceBox).value<QAudioDevice>(); + int currentDeviceIndex = 0; + + ui->audioDeviceBox->clear(); + + ui->audioDeviceBox->addItem(tr("Default"), {}); + for (const auto &device : m_mediaDevices->audioInputs()) { + const auto name = device.description(); + ui->audioDeviceBox->addItem(name, QVariant::fromValue(device)); + + if (device.id() == currentDevice.id()) + currentDeviceIndex = ui->audioDeviceBox->count() - 1; + } + + ui->audioDeviceBox->setCurrentIndex(currentDeviceIndex); +} + void AudioRecorder::updateFormats() { if (m_updatingFormats) diff --git a/examples/multimedia/audiorecorder/audiorecorder.h b/examples/multimedia/audiorecorder/audiorecorder.h index 47d7ed1e2..2ce71c162 100644 --- a/examples/multimedia/audiorecorder/audiorecorder.h +++ b/examples/multimedia/audiorecorder/audiorecorder.h @@ -14,6 +14,7 @@ namespace Ui { class AudioRecorder; } class QAudioBuffer; +class QMediaDevices; QT_END_NAMESPACE class AudioLevel; @@ -38,6 +39,7 @@ private slots: void updateProgress(qint64 pos); void displayErrorMessage(); + void updateDevices(); void updateFormats(); private: @@ -48,6 +50,8 @@ private: QMediaCaptureSession m_captureSession; QMediaRecorder *m_audioRecorder = nullptr; + QMediaDevices *m_mediaDevices = nullptr; + QList<AudioLevel *> m_audioLevels; bool m_outputLocationSet = false; bool m_updatingFormats = false; |