【问题标题】:How to make QSound synchronously?如何同步制作QSound?
【发布时间】:2017-07-15 13:42:57
【问题描述】:

如何同步制作QSound?

我有一个退出按钮。如果我点击它,我想播放声音然后退出程序。 QSound 是异步的,不知道怎么同步。

【问题讨论】:

    标签: c++ qt audio exit


    【解决方案1】:

    您实际上并不需要同步播放声音。任何可能阻塞 GUI 线程超过 0.10 秒的事情都不应该在那里完成,请查看here 了解更多信息。

    由于您愿意在用户单击退出按钮时播放声音,我认为使用QSoundEffect 更适合您的情况,来自docs

    此类允许您以通常较低延迟的方式播放未压缩的音频文件(通常是 WAV 文件),并且适用于响应用户操作的“反馈”类型的声音(例如虚拟键盘声音、弹出对话框或游戏声音的正面或负面反馈)。

    QSoundEffect 有一个信号playingChanged(),只有在声音播放完毕后,您才能利用该信号关闭应用程序。我不知道为什么QSound 没有类似的信号。

    这是一个解释如何完成的最小示例:

    #include <QtWidgets>
    #include <QtMultimedia>
    
    class Widget : public QWidget {
    public:
        explicit Widget(QWidget* parent= nullptr):QWidget(parent) {
            //set up layout
            layout.addWidget(&exitButton);
            //initialize sound effect with a sound file
            exitSoundEffect.setSource(QUrl::fromLocalFile("soundfile.wav"));
            //play sound effect when Exit is pressed
            connect(&exitButton, &QPushButton::clicked, &exitSoundEffect, &QSoundEffect::play);
            //close the widget when the sound effect finishes playing
            connect(&exitSoundEffect, &QSoundEffect::playingChanged, this, [this]{
                if(!exitSoundEffect.isPlaying()) close();
            });
        }
        ~Widget() = default;
    private:
        QVBoxLayout layout{this};
        QPushButton exitButton{"Exit"};
        QSoundEffect exitSoundEffect;
    };
    
    //sample application
    int main(int argc, char* argv[]){
        QApplication a(argc, argv);
    
        Widget w;
        w.show();
    
        return a.exec();
    }
    

    请注意,上述解决方案在音效播放完毕之前不会关闭窗口。

    另一种方法,对应用程序用户来说似乎更具响应性,即关闭窗口,在窗口关闭时播放声音,然后在播放完毕后退出应用程序。但这需要在应用程序级别关闭最后一个窗口时禁用隐式退出 (quitOnLastWindowClosed)。

    由于禁用隐式退出,您必须在程序的每个可能的退出路径上添加qApp-&gt;quit();。这是一个显示第二种方法的示例:

    #include <QtWidgets>
    #include <QtMultimedia>
    
    class Widget : public QWidget {
    public:
        explicit Widget(QWidget* parent= nullptr):QWidget(parent) {
            //set up layout
            layout.addWidget(&exitButton);
            //initialize sound effect with a sound file
            exitSoundEffect.setSource(QUrl::fromLocalFile("soundfile.wav"));
            //play sound effect and close widget when exit button is pressed
            connect(&exitButton, &QPushButton::clicked, &exitSoundEffect, &QSoundEffect::play);
            connect(&exitButton, &QPushButton::clicked, this, &Widget::close);
            //quit application when the sound effect finishes playing
            connect(&exitSoundEffect, &QSoundEffect::playingChanged, this, [this]{
                if(!exitSoundEffect.isPlaying()) qApp->quit();
            });
        }
        ~Widget() = default;
    private:
        QVBoxLayout layout{this};
        QPushButton exitButton{"Exit"};
        QSoundEffect exitSoundEffect;
    };
    
    //sample application
    int main(int argc, char* argv[]){
        QApplication a(argc, argv);
        //disable implicit quit when last window is closed
        a.setQuitOnLastWindowClosed(false);
    
        Widget w;
        w.show();
    
        return a.exec();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-31
      • 2013-07-14
      • 2011-08-22
      • 2011-10-27
      • 1970-01-01
      相关资源
      最近更新 更多