播放.wav与QSound (Qt)没有声音存在

play .wav with QSound (Qt) no sound exist

本文关键字:声音 存在 Qt wav QSound 播放      更新时间:2023-10-16

我尝试使用QSound::play在Qt中播放。wav文件

我试过这个代码:

QSound::play("airplane.wav");

构建时没有错误,但运行时没有声音?!

这给我敲响了警钟,所以我找到了用来处理声音的代码。我们的平台是Windows,所以这是适合我们的。我把这些都写进了玩家课。我给自己的笔记说,QSound需要平台格式的绝对路径(通过检查QSound代码找到)。试着像这样获取文件路径

// (note the "sSoundPath" variable is set to where we store our sound files).
static const QString sSoundPath("./resources/sounds/");

其他地方…

//  QSound wants absolute paths, in platform format
QFileInfo fileInfo(soundFile);
if (fileInfo.isRelative())
{
    //  we assume one of our own sound files in a relative path
    fileInfo.setFile(sSoundPath + soundFile);
    fileInfo.makeAbsolute();
}
if (!fileInfo.exists())
{
    return false;
}
mSoundFile = QDir::toNativeSeparators(fileInfo.filePath());

现在你可以继续并尝试播放声音文件

我在一个类似的问题上花了无数天。基本上,我发现QSound不支持44100 Hz采样率的波文件。查看我在QT5上的发现QSound不能播放所有的波文件

旁注,QSound不支持QT资源,如果你正在使用一个。一种解决方法是将资源复制到一个文件中,然后使用QSound从硬盘驱动器播放该文件。