2017-07-15 20 views
1

QSoundを同期させるにはどうすればいいですか?QSoundを同期させるには?

私には終了ボタンがあります。私がそれをクリックすると、サウンドを再生し、プログラムを終了します。 QSoundは非同期であり、同期させる方法はわかりません。

答えて

2

あなたは本当に音を同期して再生する必要はありません。 GUIスレッドを0.10秒以上ブロックするものは、そこで実行しないでください。詳細はhereをご覧ください。

ユーザーが終了ボタンをクリックしたときにサウンドを再生して喜んでいるので、私はdocsから、QSoundEffectを使用すると、あなたのケースのために優れていると思う:

このクラスでは、あなたは非圧縮オーディオファイルを再生することができますは、ユーザアクション(例えば、仮想キーボード音、ポップアップダイアログのための正または負のフィードバック、またはゲーム音)に応答して「フィードバック」タイプの音に適しています。

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->quit();を追加する必要があります。 2番目のアプローチを示す例を次に示します。

#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(); 
} 
+0

問題はQSoundEffectで解決されました。ありがとうございます。 –

関連する問題