以下回复参考:皆我百晓生、券券喵儿等免费微信小程序作答:
这个问题可能是由于音频文件的缓冲导致的。你可以尝试在播放音频之前预加载音频文件,以确保音频数据已经准备好。你可以使用QSoundEffect::setBuffer()方法来实现这一点。以下是一个示例:
#include <QSoundEffect>
#include <QUrl>
#include <QFile>
#include <QByteArray>
// ...
m_soundqrcode = new QSoundEffect();
QString filePath = ":/sound/sound/qrcode.wav";
QUrl url(filePath);
QFile file(url.toLocalFile());
if (file.open(QIODevice::ReadOnly)) {
QByteArray data = file.readAll();
file.close();
m_soundqrcode->setSource(QUrl::fromLocalFile(filePath));
m_soundqrcode->setLoopCount(1.0f);
m_soundqrcode->setBuffer(data); // 预加载音频数据
} else {
qDebug() << "Failed to open file:" << filePath;
}
// 点击时播放
m_soundqrcode->play();
这样,当你点击按钮时,音频数据应该已经准备好并立即播放,不会出现延迟。