在Qt6中以及取消了QSound,可以用其他类来替代,比如QSoundEffect。
QSound和QSoundEffect整体来说功能差不多,首先需要在.pro文件中加上multimedia,比如:
QT += core gui multimedia
QSoundEffect * startSound = new QSoundEffect(this);
tartSound->setSource(QUrl::fromLocalFile("目录"));
但是还有一些差别,比如QSoundEffec是没有setLoop,可以用setLoopCount
startSound->setLoopCount(5);
QSound无限循环是用-1来表示的,setLoopCount不可以,需要用特殊的表示方法:
startSound->setLoopCount(QSoundEffect::Infinite);
需要触发音效的时候,直接->play()就好。
startSound->play();