Qt World Summit: Register Today!

Is it possible to play a sound and the moment it ends do something?

  • I have a button and when you press it will play a sound and say hello.

    @ if(system("canberra-gtk-play -f /usr/share/Sound.ogg&"))
    cout << "Could't play ~/usr/share/Sound.ogg. Check file existence.\n";
    cout << "Hello!" << endl;@

    But the problem is that the moment that you press the button it will play the sound and say hello before the sound ends... So how is it possible to say hello the moment that the sound ends.

  • I dont see any signal in the "docs":http://doc.qt.nokia.com/4.7/qsound.html , so i suspect that you have to track it yourself. You can set a timer connected with a function wich checks if isfinished == true. (on windows it always returns true :( ). And if so do anything you like.

  • How to check if isfinished == true? Can u be more precisely?
    I am using Ubuntu so no problem, i think, with the true or false.

  • Some not tested pseudo code:
    In ".h" put QSound *sound;
    void "constructor"
    QTimer *t = new QTimer(this); //create timer
    sound("path to sound");
    t->start(1000); //1 second timer

    void testFinished()
    if (sound->isFinished) {
    cout << "Finished" << endl;

  • Errors on your code:
    ‘((ArbylaPlayer*)this)->ArbylaPlayer::sound’ cannot be used as a function
    ‘((ArbylaPlayer*)this)->ArbylaPlayer::sound->QSound::isFinished’ to ‘bool’

    "Here":http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qsound.html is the QSound class reference but the


    and the

    @QSound bells("mysounds/bells.wav");

    doesn't work.. i think it is playing in the background but you have no sound.. I have include QSound in both .h and .cpp files but still no sound..

  • As i said, it was a pseudo not tested code. It was only to gave you an idea. :)

    Please post your code.

    Note: Use the "docs":http://doc.qt.nokia.com/4.7/qsound.html provided by Qt.

  • Here is my code in cpp file
    #include "arbylaplayer.h"
    #include "ui_arbylaplayer.h"
    #include "QSound"

    ArbylaPlayer::ArbylaPlayer(QWidget *parent) :
    ui(new Ui::ArbylaPlayer)

    delete ui;

    No errors but u do not hear any sound.. Any ideas?

  • from the docs:

    The Network Audio System is used if available, otherwise all operations work silently. NAS supports WAVE and AU files."

  • Yes and what should i do for that? At the class reference about Qt for Embedded Linux it says: A built-in mixing sound server is used, accessing /dev/dsp directly. Only the WAVE format is supported.

    I have a .wav file but still no sound.. :/

  • I don't think that the sound really plays.. It must be something else.. I putted

    @if (!QSound::isAvailable()) {
    cout << "No sound" << endl;

    and the outpout is "No sound"... So something is missing.. I installed from Synaptic: nas-local server, nas-bin and libaudio-dev but i think somehow i must reconfigure Qt with NAS sound support but i do not know how to do this.. Any ideas?

Log in to reply