【问题标题】:QT5.5 QSound isFinshedQT5.5 QSound isFinshed
【发布时间】:2016-02-19 10:33:59
【问题描述】:

我正忙着做一个混响算法。在使用QSound 时,我发现了一些问题。

首先,像这样尝试QSound::play() 时不会播放声音:

/// Play output .wav file.
QSound sound("C:/Users/mvdelft/Documents/Reverb_configurator/output.wav", this);
sound.play();

只有当我使用QSound::playQString 文件)重新指定路径时,它才会播放声音,如下所示:

/// Play output .wav file.
QSound sound("C:/Users/mvdelft/Documents/Reverb_configurator/output.wav", this);
sound.play("C:/Users/mvdelft/Documents/Reverb_configurator/output.wav");

我遇到的一个相关问题与函数 bool QSound::isFinshed() 有关,它对我不起作用。代码:

 /// Play output .wav file.
QSound sound("C:/Users/mvdelft/Documents/Reverb_configurator/output.wav", this);
sound.play("C:/Users/mvdelft/Documents/Reverb_configurator/output.wav");
sound.setLoops(10);

/// Check is sound is finished
while (!sound.isFinished()){}

ui->listWidget->addItem("Finished playing sound");
}/// End of scope

【问题讨论】:

    标签: c++ qt5.5 qtmultimedia


    【解决方案1】:

    在第一个版本中,您在堆栈上创建一个带有文件的QSound 对象,开始播放它,然后立即销毁它。这将停止声音播放,因此您将听不到任何声音。

    在第二个版本中,QSound::play(const QString &) 是一个静态方法。它将在后台播放声音。这就是为什么你听到一些东西。 使用静态方法,对setLoopsisFinished 的调用将不起作用。另外,busy loop (while (!sound.isFinished()) ;) 非常糟糕,因为它会消耗 100% 的 CPU,并且可能会阻塞播放声音。

    为了让声音正常工作,您应该在堆上创建它,并定期检查计时器事件上的isFinished()。但是,我建议QSoundEffect,因为它可以让您拥有更多控制权。最重要的是,playingChanged() 信号会在播放结束时通知您,而无需经常检查。

    大纲:

    void MyObject::playSomeSound() {
       QSoundEffect *s = new QSoundEffect(this);
       connect(s, SIGNAL(playingChanged()), this, SLOT(soundPlayingChanged()));
       s->setSource("C:/Users/mvdelft/Documents/Reverb_configurator/output.wav");
       s->setLoopCount(10);
       s->play();
    }
    
    void MyObject::soundPlayingChanged() {
       QSoundEffect *s = qobject_cast<QSoundEffect *> (sender());
       // Will also be called when playing was started, so check if really finished
       if (!s->isPlaying()) {
          s->deleteLater();
    
          // Do what you need to do when playing finished
       }
    }
    

    【讨论】:

    • 感谢您的快速评论,现在可以使用了!作为具有平均编程技能的人,它仍然让我对记忆问题感到困惑。不过谢谢!
    猜你喜欢
    • 1970-01-01
    • 2013-02-11
    • 1970-01-01
    • 2020-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多