首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >QT5.5 QSound isFinshed

QT5.5 QSound isFinshed
EN

Stack Overflow用户
提问于 2016-02-19 10:33:59
回答 1查看 490关注 0票数 0

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

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

代码语言:javascript
复制
/// Play output .wav file.
QSound sound("C:/Users/mvdelft/Documents/Reverb_configurator/output.wav", this);
sound.play();

只有当我用QSound::play (QString文件)给出路径时,它才能播放声音:

代码语言:javascript
复制
/// 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()有关,它对我不起作用。代码:

代码语言:javascript
复制
 /// 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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-19 11:37:22

在第一个版本中,使用文件在堆栈上创建一个QSound对象,开始播放它,然后立即销毁它。这将停止声音播放,所以你不会听到任何东西。

在第二个版本中,QSound::play(const QString &)是一个静态方法。它将播放背景中的声音。这就是为什么你听到了什么。使用静态方法,对setLoopsisFinished的调用将无法工作。另外,繁忙的循环(while (!sound.isFinished()) ;)非常糟糕,因为它将消耗100%的CPU,并且可能阻止播放声音。

要使声音工作,您应该在堆上创建它,并在计时器事件上定期检查isFinished()。但是,我建议使用QSoundEffect,因为它给了您更多的控制权。最重要的是,playingChanged()信号,这将通知你什么时候比赛已经结束,而不需要不断检查。

大纲:

代码语言:javascript
复制
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
   }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35503124

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档