我正忙着做混响算法。在使用QSound时,我发现了一些问题。
首先,在尝试这样的QSound::play()时,声音不会播放:
/// Play output .wav file.
QSound sound("C:/Users/mvdelft/Documents/Reverb_configurator/output.wav", this);
sound.play();只有当我用QSound::play (QString文件)给出路径时,它才能播放声音:
/// 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发布于 2016-02-19 11:37:22
在第一个版本中,使用文件在堆栈上创建一个QSound对象,开始播放它,然后立即销毁它。这将停止声音播放,所以你不会听到任何东西。
在第二个版本中,QSound::play(const QString &)是一个静态方法。它将播放背景中的声音。这就是为什么你听到了什么。使用静态方法,对setLoops和isFinished的调用将无法工作。另外,繁忙的循环(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
}
}https://stackoverflow.com/questions/35503124
复制相似问题