Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Proper way playing oga files as sound effects



  • I am developing a GUI application under Ubuntu Linux with Qt5.
    I want to use sounds from "/usr/share/sounds/freedesktop/" as sound effects, which contains .oga files. However, the QSoundEffect class seems to understand only .wav files. qDebug() << QSoundEffect::supportedMimeTypes(); outputs

    ("audio/x-wav", "audio/vnd.wave")
    

    and the application prints QSoundEffect(pulseaudio): Error decoding source at initialization.
    Is there a way to play these files?

    I use the QSoundEffect as in the code snippet below. Is this the proper way to use the class?
    Is using QMediaPlayer instead of QSoundEffect an overkill?

    class MyClass: public QDialog {
    public:
        MyClass(QObject * parent = nullptr): QDialog(parent) {
    
        QSoundEffect * ok = new QSoundEffect(this);
        ok->setSource("/path/to/file.oga");
        sound_effects_["ok"] = ok;
       /*Initialize other effects*/
    
        connect(this, &MyClass::PlaySound, this, &MyClass::Play);
    }
    
    void AFunctionFromADiffernetThread(){
         // Do other things
        if(success)
            emit PlaySound("ok");
    }
    
    signals:
        void PlaySound(const QString & name);
    
    private slots:
      void Play(const QString & name){
        sound_effects_[name]->play();
     }
    private:
        QMap<QString, QSoundEffect *> sound_effects_;
    }
    

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    The details of QSoundEffect explain that only uncompressed formats are supported, typically wav files.


Log in to reply