Bekomme bei Qt6 das Abspielen von wav Dateien nicht ans Laufen

Alles rund um die Programmierung mit Qt
Antworten
u640
Beiträge: 1
Registriert: 16. April 2023 16:44

Bekomme bei Qt6 das Abspielen von wav Dateien nicht ans Laufen

Beitrag von u640 »

Ich versuche gerade ein komplexes, histotisch gewachsenes GPLv3 Projekt von Qt5 auf Qt6 umzustellen, und bekomme es irgendwie hartnäckig nicht hin, mit Qt6.5 wav Dateien auf das default Audio Device abzuspielen. Mit Qt5 war das ganz einfach. Der folgende Code funktioniert dort prima:

Code: Alles auswählen

#include <QSound>
#include <QAudioOutput>
#include <QDir>
#include <QCoreApplication>

{
    QAudioOutput device(QAudioDeviceInfo::defaultOutputDevice());
    QString homePath = QDir::homePath();
    if (play_) {
       QSound::play(binPath + "/sounds/1.wav");
    }
}
Für Qt6 habe ich folgende 2 Code-Varianten ausprobiert, die beide kompilieren, aber es wird leider kein Audio ausgegeben (auf keiner der vorhandenen Audioausgänge):

1.)

Code: Alles auswählen

#include <QSoundEffect>
#include <QAudioOutput>
#include <QMediaDevices>
#include <QAudioDevice>
#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();
    }
}

2.)

Code: Alles auswählen

#include <QSoundEffect>
#include <QAudioOutput>
#include <QMediaDevices>
#include <QAudioDevice>
#include <QAudioSink>
#include <QDir>
#include <QCoreApplication>

{
  QAudioDevice device(QMediaDevices::defaultAudioOutput());
  QString binPath = QCoreApplication::applicationDirPath();
  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(binPath + "/sounds/1.wav");
            effect.open(QIODevice::ReadOnly);
            audio->start(&effect);
    }
}
Hat irgendjemand von euch eine Idee warum das nicht funktioniert und wie ich es zum Laufen bekommen kann?

Noch ein Hinweis: Ich bin kein gelernter Programmierer.
Antworten