Solved Record Audio in Qt6
-
I want to record audio. In qt6 the QAudioRecorder isn't available anymore, instead the QMediaRecorder should be used.
// RECORD QMediaCaptureSession* session = new QMediaCaptureSession(); QAudioInput* audioInput = new QAudioInput(); QMediaRecorder* recorder = new QMediaRecorder(); session->setAudioInput(audioInput); session->setRecorder(recorder); recorder->setMediaFormat(QMediaFormat::Wave); recorder->setOutputLocation(QUrl::fromLocalFile("test")); recorder->setAudioSampleRate(48000); recorder->setAudioChannelCount(1); recorder->record(); QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), recorder, SLOT(stop())); timer->start(3500);
But the recorded file is always zero. The code is from the examples and I think it is new in Qt6.
-
changed to matlab --> https://github.com/Christoph-Lauer/Transfer-Function-Extractor-Matlab
-
Cannot say I know anything about this, but this line looks wrong:
recorder->setOutputLocation(QUrl::fromLocalFile("test"));
If for nothing else, "test" does not have an extension.
-
@admkrk said in Record Audio in Qt6:
If for nothing else, "test" does not have an extension
A file does not have to have an extension, not even on Windows.
-
@QtAndrew There are several signals which you should connect to slots and see what happens: https://doc.qt.io/qt-5/qaudioinput.html#stateChanged and https://doc.qt.io/qt-5/qmediarecorder.html#error-1
Also, this could be helpful: https://doc.qt.io/qt-5/qaudioinput.html#error
-
@admkrk thanks for your response --> I tested all other combinations, the extension is added automatically in macos and the file is placed in the music folder. This seems not to be the problem.
-
@jsulm Thanks --> i didn't understand this LOC from the examples you suggest.
connect(mediaRecorder, QOverload<QMediaRecorder::Error>::of(&QMediaRecorder::error), [=](QMediaRecorder::Error error){ /* ... */ });
getting an error, what did "of" mean? sorry
-
@QtAndrew
TheQOverload<...>::of
is to pick out a particular overload. The docs explain:Note: Signal error is overloaded in this class. To connect to this signal by using the function pointer syntax, Qt provides a convenient helper for obtaining the function pointer as shown in this example:
See also https://doc.qt.io/qt-5/qtglobal.html#qOverload.
You should show the error message.
-
@JonB the examples are full of spelling mistakes. QOverload vs. qOverload. By the way, i am not able to put it into my example. Could it be that macos does not support the recording function?
-
@QtAndrew said in Record Audio in Qt6:
the examples are full of spelling mistakes. QOverload vs. qOverload.
Why not read the docs instead complaining for no reason? Both versions are valid and it's perfectly explained why there are two version. But looks like you're too lazy to read.
-
@QtAndrew
There are no spelling mistakes.qOverload
is not the same asQOverload
. The page I referred you to shows that, and shows examples.By the way, i am not able to put it into my example.
Twice you have said "it doesn't work" or "getting an error". I have asked you to show your code and paste the error you get. How can we answer without those details?
-
// RECORD QMediaCaptureSession* session = new QMediaCaptureSession(); QAudioInput* audioInput = new QAudioInput(); QMediaRecorder* recorder = new QMediaRecorder(); session->setAudioInput(audioInput); session->setRecorder(recorder); recorder->setMediaFormat(QMediaFormat::Wave); recorder->setOutputLocation(QUrl::fromLocalFile("test")); recorder->setAudioSampleRate(48000); recorder->setAudioChannelCount(1); recorder->record(); connect(recorder, QOverload<QMediaRecorder::Error>::of(&QMediaRecorder::error), [=](QMediaRecorder::Error error){ /* ... */ }); QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), recorder, SLOT(stop())); timer->start(3500);
ERROR: 1.) Does not Record any audio but the file is created (macos ARM64)
ERROR: 2.)/Users/christoph/Documents/DEVELOPMENT/Impulse-Response-Extractor/IRExtractor/irextractor.cpp:44: Fehler: no matching function for call to 'of'
../IRExtractor/irextractor.cpp:44:23: error: no matching function for call to 'of'
connect(recorder, QOverloadQMediaRecorder::Error::of(&QMediaRecorder::error),
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~/Volumes/Externe-SSD/Development/Qt/6.2.0/macos/lib/QtCore.framework/Headers/qglobal.h:1345:27: note: candidate template ignored: failed template argument deduction
static constexpr auto of(R (T::*ptr)(Args...) const) noexcept -> decltype(ptr)
^ -
@QtAndrew Since you're on Qt6 you should use https://doc-snapshots.qt.io/qt6-dev/qmediarecorder.html#errorOccurred
And don't forget to print the error in the slot you connect to this signal. -
This post is deleted! -
@QtAndrew said in Record Audio in Qt6:
why does this code sniplet stand on 6.2 QMediaRecorder ?
It does not. This code snippet is in Qt5 documentation, not in Qt6. I noticed from your compile log that you're using Qt6.2 and provided you the link to Qt6 documentation.
Don't know what is unclear in the documentation... -
@QtAndrew said in Record Audio in Qt6:
I think i change the platorform.
Please do so. Maybe you find a toolkit where the version is automatically guessed by your browser when you search for help.
-
This post is deleted! -
I tried:
1.) Threads
2.) Timers
3.) start the recording and connect the stop() to the player end of media slot.no success...
void IRExtractor::on_sweepButton_clicked() { // PLAY MyThread* t = new MyThread(); t->start(); // RECORD QMediaCaptureSession* session = new QMediaCaptureSession(); QAudioInput* audioInput = new QAudioInput(); QMediaRecorder* recorder = new QMediaRecorder(); session->setAudioInput(audioInput); session->setRecorder(recorder); recorder->setMediaFormat(QMediaFormat::Wave); recorder->setOutputLocation(QUrl::fromLocalFile("test")); recorder->setAudioSampleRate(48000); recorder->setAudioChannelCount(1); recorder->record(); QThread::msleep(3250); recorder->stop(); } void MyThread::run() { QMediaPlayer* player = new QMediaPlayer(); QAudioOutput* audioOutput = new QAudioOutput; audioOutput->setVolume(0.1); player->setAudioOutput(audioOutput); player->setSource(QUrl("qrc:/res/sweep.wav")); player->play(); }
This version didn't play the audio.
The Project is now on GitHub: https://github.com/Christoph-Lauer/Impulse-Response-Extractor
-
This post is deleted! -
@QtAndrew
I do not know know anything about this, so no point asking me any further! But from your code are you supposed to some error checking as per https://forum.qt.io/topic/130588/qmediaplay-stream-does-not-wokk/2 ? -
changed to matlab --> https://github.com/Christoph-Lauer/Transfer-Function-Extractor-Matlab