How to port QAudioOutput device(QAudioDeviceInfo::defaultOutputDevice()); to Qt6?
-
I am currently porting a quite complex project from qt5 to qt6, and cannot figure out how to correctly port this line:
QAudioOutput device(QAudioDeviceInfo::defaultOutputDevice());
With qt5, this very simple code works pretty well to play a smal wav file:
#include <QSound> #include <QAudioOutput> #include <QDir> #include <QCoreApplication> { QAudioOutput device(QAudioDeviceInfo::defaultOutputDevice()); QString homePath = QDir::homePath(); if (play_) { QSound::play(binPath + "/sounds/1.wav"); } }
With qt6, I tried the following, ...
#include <QSoundEffect> #include <QAudioOutput> #include <QMediaDevices> #include <QDir> #include <QCoreApplication> { QAudioDevice info(QMediaDevices::defaultAudioOutput()); QString homePath = QDir::homePath(); if (play_) { QSoundEffect effect; effect.setSource(QUrl::fromLocalFile(binPath + "/sounds/1.wav")); effect.play(); } }
... but I am getting the following two compilation errors about thís line:
error: variable 'QAudioDevice info' has initializer but incomplete type
error: invalid use of incomplete type 'class QAudioDevice'Any idea?
-
I am currently porting a quite complex project from qt5 to qt6, and cannot figure out how to correctly port this line:
QAudioOutput device(QAudioDeviceInfo::defaultOutputDevice());
With qt5, this very simple code works pretty well to play a smal wav file:
#include <QSound> #include <QAudioOutput> #include <QDir> #include <QCoreApplication> { QAudioOutput device(QAudioDeviceInfo::defaultOutputDevice()); QString homePath = QDir::homePath(); if (play_) { QSound::play(binPath + "/sounds/1.wav"); } }
With qt6, I tried the following, ...
#include <QSoundEffect> #include <QAudioOutput> #include <QMediaDevices> #include <QDir> #include <QCoreApplication> { QAudioDevice info(QMediaDevices::defaultAudioOutput()); QString homePath = QDir::homePath(); if (play_) { QSoundEffect effect; effect.setSource(QUrl::fromLocalFile(binPath + "/sounds/1.wav")); effect.play(); } }
... but I am getting the following two compilation errors about thís line:
error: variable 'QAudioDevice info' has initializer but incomplete type
error: invalid use of incomplete type 'class QAudioDevice'Any idea?
@Urbi said in How to port QAudioOutput device(QAudioDeviceInfo::defaultOutputDevice()); to Qt6?:
Any idea?
Basic c++ (or any other language) - when you want to use a class, you have to make sure that the compiler knows it by including the correct stuff: https://doc.qt.io/qt-6/qaudiodevice.html
-
@Urbi said in How to port QAudioOutput device(QAudioDeviceInfo::defaultOutputDevice()); to Qt6?:
Any idea?
Basic c++ (or any other language) - when you want to use a class, you have to make sure that the compiler knows it by including the correct stuff: https://doc.qt.io/qt-6/qaudiodevice.html
@Christian-Ehrlicher
If you know all this so well, then just tell me WHAT is missing WHERE. Is that too much? -
@Christian-Ehrlicher
If you know all this so well, then just tell me WHAT is missing WHERE. Is that too much?@Urbi said in How to port QAudioOutput device(QAudioDeviceInfo::defaultOutputDevice()); to Qt6?:
WHAT is missing WHERE. Is that too much?
Is reading behind the first line of a documentation really so complicated?
"Header: #include <QAudioDevice>"
-
@Urbi said in How to port QAudioOutput device(QAudioDeviceInfo::defaultOutputDevice()); to Qt6?:
WHAT is missing WHERE. Is that too much?
Is reading behind the first line of a documentation really so complicated?
"Header: #include <QAudioDevice>"
@Christian-Ehrlicher
Thanks. Now it compiles, but I can hear no audio. May there be anything wrong in the following line?effect.setSource(QUrl::fromLocalFile(binPath + "/sounds/1.wav"));
-
@Christian-Ehrlicher
Thanks. Now it compiles, but I can hear no audio. May there be anything wrong in the following line?effect.setSource(QUrl::fromLocalFile(binPath + "/sounds/1.wav"));
@Urbi said in How to port QAudioOutput device(QAudioDeviceInfo::defaultOutputDevice()); to Qt6?:
o. May there be anything wrong in the following line?
Why not check out by yourself. QFile::exits() for example.
-
@Urbi said in How to port QAudioOutput device(QAudioDeviceInfo::defaultOutputDevice()); to Qt6?:
o. May there be anything wrong in the following line?
Why not check out by yourself. QFile::exits() for example.
@Christian-Ehrlicher
The file definitely exists. Even when I adress here the absolute path, no ausio can be heard. -
I am currently porting a quite complex project from qt5 to qt6, and cannot figure out how to correctly port this line:
QAudioOutput device(QAudioDeviceInfo::defaultOutputDevice());
With qt5, this very simple code works pretty well to play a smal wav file:
#include <QSound> #include <QAudioOutput> #include <QDir> #include <QCoreApplication> { QAudioOutput device(QAudioDeviceInfo::defaultOutputDevice()); QString homePath = QDir::homePath(); if (play_) { QSound::play(binPath + "/sounds/1.wav"); } }
With qt6, I tried the following, ...
#include <QSoundEffect> #include <QAudioOutput> #include <QMediaDevices> #include <QDir> #include <QCoreApplication> { QAudioDevice info(QMediaDevices::defaultAudioOutput()); QString homePath = QDir::homePath(); if (play_) { QSoundEffect effect; effect.setSource(QUrl::fromLocalFile(binPath + "/sounds/1.wav")); effect.play(); } }
... but I am getting the following two compilation errors about thís line:
error: variable 'QAudioDevice info' has initializer but incomplete type
error: invalid use of incomplete type 'class QAudioDevice'Any idea?
@Urbi said in How to port QAudioOutput device(QAudioDeviceInfo::defaultOutputDevice()); to Qt6?:
{
QAudioDevice info(QMediaDevices::defaultAudioOutput());
QString homePath = QDir::homePath();
if (play_) {
QSoundEffect effect;
effect.setSource(QUrl::fromLocalFile(binPath + "/sounds/1.wav"));
effect.play();
}
}Then the next c++ question - how long does QSoundEffect live?
-
@Urbi said in How to port QAudioOutput device(QAudioDeviceInfo::defaultOutputDevice()); to Qt6?:
{
QAudioDevice info(QMediaDevices::defaultAudioOutput());
QString homePath = QDir::homePath();
if (play_) {
QSoundEffect effect;
effect.setSource(QUrl::fromLocalFile(binPath + "/sounds/1.wav"));
effect.play();
}
}Then the next c++ question - how long does QSoundEffect live?
@Christian-Ehrlicher
Unfortunately, still no success. I tried also the following code, however, again no audio (checked all sinks).#include <QSoundEffect> #include <QAudioOutput> #include <QMediaDevices> #include <QAudioDevice> #include <QAudioSink> #include <QDir> #include <QCoreApplication> { QAudioDevice device(QMediaDevices::defaultAudioOutput()); QString homePath = QDir::homePath(); QAudioFormat format; format.setSampleRate(8000); format.setChannelCount(1); format.setSampleFormat(QAudioFormat::UInt8); QAudioSink* audio = new QAudioSink(format, this); connect(audio, SIGNAL(stateChanged(QAudio::State)), this, SLOT(handleStateChanged(QAudio::State))); if (play_) { QFile effect; effect.setFileName(homePath + "/sounds/1.wav"); effect.open(QIODevice::ReadOnly); audio->start(&effect); } }
-
@Christian-Ehrlicher
Unfortunately, still no success. I tried also the following code, however, again no audio (checked all sinks).#include <QSoundEffect> #include <QAudioOutput> #include <QMediaDevices> #include <QAudioDevice> #include <QAudioSink> #include <QDir> #include <QCoreApplication> { QAudioDevice device(QMediaDevices::defaultAudioOutput()); QString homePath = QDir::homePath(); QAudioFormat format; format.setSampleRate(8000); format.setChannelCount(1); format.setSampleFormat(QAudioFormat::UInt8); QAudioSink* audio = new QAudioSink(format, this); connect(audio, SIGNAL(stateChanged(QAudio::State)), this, SLOT(handleStateChanged(QAudio::State))); if (play_) { QFile effect; effect.setFileName(homePath + "/sounds/1.wav"); effect.open(QIODevice::ReadOnly); audio->start(&effect); } }
@Urbi said in How to port QAudioOutput device(QAudioDeviceInfo::defaultOutputDevice()); to Qt6?:
if (play_) { QFile effect; effect.setFileName(homePath + "/sounds/1.wav"); effect.open(QIODevice::ReadOnly); audio->start(&effect); }
lifetime?
-
@Urbi said in How to port QAudioOutput device(QAudioDeviceInfo::defaultOutputDevice()); to Qt6?:
if (play_) { QFile effect; effect.setFileName(homePath + "/sounds/1.wav"); effect.open(QIODevice::ReadOnly); audio->start(&effect); }
lifetime?
@Christian-Ehrlicher
Give me the answer. I'n not a professional programmer ... -
@Christian-Ehrlicher
Give me the answer. I'n not a professional programmer ...@Urbi said in How to port QAudioOutput device(QAudioDeviceInfo::defaultOutputDevice()); to Qt6?:
I'n not a professional programmer ...
But you should understand the basic stuff of the programming language you're using. And I asked you to think about the lifetime of the objects in the code I copied from you - so maybe im some more words: how long does
QFile effect;
live? Not that I asked the exact same question or another object some hours ago. -
@Urbi said in How to port QAudioOutput device(QAudioDeviceInfo::defaultOutputDevice()); to Qt6?:
I'n not a professional programmer ...
But you should understand the basic stuff of the programming language you're using. And I asked you to think about the lifetime of the objects in the code I copied from you - so maybe im some more words: how long does
QFile effect;
live? Not that I asked the exact same question or another object some hours ago.@Christian-Ehrlicher Christian, sorry, I am looking for a solution, not for playing games. If you knew who I am and what I have already manage to program you wouldn't treat me that way. Sorry, if this just a from some nerds... Good bye.
-
@Christian-Ehrlicher Christian, sorry, I am looking for a solution, not for playing games. If you knew who I am and what I have already manage to program you wouldn't treat me that way. Sorry, if this just a from some nerds... Good bye.
@Urbi said in How to port QAudioOutput device(QAudioDeviceInfo::defaultOutputDevice()); to Qt6?:
. Sorry, if this just a from some nerds... Good bye.
No nerds - basic c++ understanding.
QFile effect
lives until the closing bracket after audio->start() so how showaudio
be able to read anything from this QFile afterwards? -
@Christian-Ehrlicher Christian, sorry, I am looking for a solution, not for playing games. If you knew who I am and what I have already manage to program you wouldn't treat me that way. Sorry, if this just a from some nerds... Good bye.
@Christian-Ehrlicher is right, you need to know at least basic C++ to create your projects. @Urbi in your case your local
QFile effect;
variable gets out of scope (gets deleted) after the bracket, so no audio will be outputed. You must have it as a pointer and keep in mind to remove it later after you no longer need it to prevent a memory leak. You can usedelete
ordeleteLater()
. Please, check out my test code below.Code:
if (play) { QFile *effectFile = new QFile(this); effectFile->setFileName(QString("%1/%2").arg(homePath, "sounds/1.wav")); qDebug() << "Is file exists: " << effectFile->exists() << " | " << effectFile->fileName(); effectFile->open(QIODevice::ReadOnly); audio->start(effectFile); effectFile->close(); effectFile->deleteLater(); //removes the QFile object, or call - delete effectFile }
Also, you can use the
qDebug()
to check the console output for example to see if the file exists and other useful stuff. By the way, whatQt 6
version are you using for this project? -
@Christian-Ehrlicher is right, you need to know at least basic C++ to create your projects. @Urbi in your case your local
QFile effect;
variable gets out of scope (gets deleted) after the bracket, so no audio will be outputed. You must have it as a pointer and keep in mind to remove it later after you no longer need it to prevent a memory leak. You can usedelete
ordeleteLater()
. Please, check out my test code below.Code:
if (play) { QFile *effectFile = new QFile(this); effectFile->setFileName(QString("%1/%2").arg(homePath, "sounds/1.wav")); qDebug() << "Is file exists: " << effectFile->exists() << " | " << effectFile->fileName(); effectFile->open(QIODevice::ReadOnly); audio->start(effectFile); effectFile->close(); effectFile->deleteLater(); //removes the QFile object, or call - delete effectFile }
Also, you can use the
qDebug()
to check the console output for example to see if the file exists and other useful stuff. By the way, whatQt 6
version are you using for this project?@Cobra91151 It gives this compilation error: 'audio' was not declared in this scope; did you mean 'QAudio'?
-
@Christian-Ehrlicher is right, you need to know at least basic C++ to create your projects. @Urbi in your case your local
QFile effect;
variable gets out of scope (gets deleted) after the bracket, so no audio will be outputed. You must have it as a pointer and keep in mind to remove it later after you no longer need it to prevent a memory leak. You can usedelete
ordeleteLater()
. Please, check out my test code below.Code:
if (play) { QFile *effectFile = new QFile(this); effectFile->setFileName(QString("%1/%2").arg(homePath, "sounds/1.wav")); qDebug() << "Is file exists: " << effectFile->exists() << " | " << effectFile->fileName(); effectFile->open(QIODevice::ReadOnly); audio->start(effectFile); effectFile->close(); effectFile->deleteLater(); //removes the QFile object, or call - delete effectFile }
Also, you can use the
qDebug()
to check the console output for example to see if the file exists and other useful stuff. By the way, whatQt 6
version are you using for this project?@Cobra91151 Sorry, I forgot that I had already deleted the lines
QAudioFormat format; format.setSampleRate(8000); format.setChannelCount(1); format.setSampleFormat(QAudioFormat::UInt8); QAudioSink* audio = new QAudioSink(format, this);
Now it compiles and sends "something" to the default audio device. However, I just hear a crackle, and the program crashes afterwards. But at least one step ahead ...
-
@Cobra91151 It gives this compilation error: 'audio' was not declared in this scope; did you mean 'QAudio'?
It's
audio
object from your code above:QAudioSink *audio = new QAudioSink(format, this);
WhatQt 6
version do you use? Because I gotqt.multimedia.audiooutput: Failed to set up resampler
issue, which is well known bug: https://bugreports.qt.io/browse/QTBUG-108383. This issue has been fixed inQt 6.4.3
.
So, I recommend check out the console output for the same issue? -
@Cobra91151 Sorry, I forgot that I had already deleted the lines
QAudioFormat format; format.setSampleRate(8000); format.setChannelCount(1); format.setSampleFormat(QAudioFormat::UInt8); QAudioSink* audio = new QAudioSink(format, this);
Now it compiles and sends "something" to the default audio device. However, I just hear a crackle, and the program crashes afterwards. But at least one step ahead ...
Then check out your program using the debugger to find out what causes the crash (debug mode only). It should point you to the right code section.
By the way, include this code in main function:
#ifdef QT_DEBUG qputenv("QT_FATAL_WARNINGS", "1"); qputenv("QT_MESSAGE_PATTERN", "Type: %{type}\nProduct Name: %{appname}\nFile: %{file}\nLine: %{line}\nMethod: %{function}\nThreadID: %{threadid}\nThreadPtr: %{qthreadptr}\nMessage: %{message}"); #endif
Then run the debugger. It should provide more details about this issue.
-
Then check out your program using the debugger to find out what causes the crash (debug mode only). It should point you to the right code section.
By the way, include this code in main function:
#ifdef QT_DEBUG qputenv("QT_FATAL_WARNINGS", "1"); qputenv("QT_MESSAGE_PATTERN", "Type: %{type}\nProduct Name: %{appname}\nFile: %{file}\nLine: %{line}\nMethod: %{function}\nThreadID: %{threadid}\nThreadPtr: %{qthreadptr}\nMessage: %{message}"); #endif
Then run the debugger. It should provide more details about this issue.
@Cobra91151 With these parameters, the wav file is correctly played. However, the program still crashes immediately after playing is finished.
#include <QSoundEffect> #include <QAudioOutput> #include <QMediaDevices> #include <QAudioDevice> #include <QAudioSink> #include <QCoreApplication> #include <QDir> { QAudioDevice info(QMediaDevices::defaultAudioOutput()); QString homePath = QDir::homePath(); QAudioFormat format; format.setSampleRate(48000); format.setChannelCount(1); format.setSampleFormat(QAudioFormat::Int16); QAudioSink* audio = new QAudioSink(format, this); connect(audio, SIGNAL(stateChanged(QAudio::State)), this, SLOT(handleStateChanged(QAudio::State))); if (play_) { QFile *effectFile = new QFile(this); effectFile->setFileName(QString("%1/%2").arg(homePath, "/sounds/1.wav")); qDebug() << "Is file exists: " << effectFile->exists() << " | " << effectFile->fileName(); effectFile->open(QIODevice::ReadOnly); audio->start(effectFile); effectFile->close(); effectFile->deleteLater(); //removes the QFile object, or call - delete effectFile } }