问题描述
我正在将基于 QT4.X 的应用迁移到 QT5.QT5 中删除了声子支持,因此我将播放波形文件的代码更改为使用 QSound.
I am in the midst of migrating our app based on QT4.X to QT5. Phonon support has been removed in QT5, so I have changed my code that plays a wave file to use QSound.
更改非常简单.我只需要使用 QSound,它现在位于多媒体库中.代码如下:
Change is pretty straightforward. I just had to use QSound, which is now located in Multimedia library. Here is the code:
QSound::play("small_wave_file.wav");
对于我的大部分波形文件,这都很好;但是,对于我的波形文件(采样率为 44100Hz),它不起作用.
For most of my wave files, this works just fine; however, for my wave file(with 44100Hz sample rate) it does not work.
可以在此处找到官方 QT 错误.
Official QT bug can be found here.
更新:此错误已在 qt 5.1 中修复
UPDATE: This bug has been fixed in qt 5.1
推荐答案
事实证明,某些 wave 文件会混淆 QSound.仍然不确定究竟是什么导致了这个问题.当我在 Audacity 中加载我的波形文件,然后将其导出回不同的波形文件而没有任何更改(相同的采样率......).QSound 可以很好地播放文件.
It turns out that some wave files confuse QSound. Still not sure exactly what causes the issue. When I loaded my wave file in Audacity, and then exported it back to a different wave file without any changes(same sample rate...). QSound played the file just fine.
在 MacOSX 中,当我在有问题的波形文件上单击获取信息"时,波形文件中的一般波形信息记录不可用;所以也许 QSound 无法从中获取采样率信息;并且因为它不知道从波形文件中期望哪个采样率?
In MacOSX when I click "Get Info" on the problematic wave file, general wave info record from the wave file was not available; so perhaps QSound was unable to get sample rate information from; and because it did not know which sample rate to expect from the wave file?
有趣的部分是 iTunes 可以很好地播放原始文件,并且以某种方式具有正确的采样率.Phonon 也用来播放原始文件.
The interesting part is that iTunes played the original file just fine, and it had the correct sample rate somehow. Also Phonon used to play the original file as well just fine.
无论如何,希望这对一些对 QSound::play() 方法有问题的人有所帮助.
Anyhow, hopefully this helps with some people that had issues with QSound::play() method.
更新:由于 QSound::play() 在 mac 上有很多问题,我选择使用本机 NSSound 在 mac 上从 QT 应用程序播放我的波形文件.代码如下:
UPDATE: Since QSound::play() was very buggy on the mac, I opted to use the native NSSound to play my wave files from QT application on the mac. Here is the code:
void play_sound( const char* file)
{
NSSound *sound = [[NSSound alloc] initWithContentsOfFile:[NSString stringWithUTF8String:file] byReference:NO];
[sound play];
[sound release];
}
这篇关于QT5 QSound 无法播放所有波形文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!