QML:SoundEffect不起作用,音频起作用 [英] QML: SoundEffect not working, Audio does
问题描述
我有两个用于音频的自定义QML元素,一个用于播放音乐(并扩展了QML Audio元素),一个用于播放音效(扩展了QML的SoundEffect元素).
I have two custom QML elements for audio, one for playing music (and extends the QML Audio element) and one for playing sound effects (which extends QML's SoundEffect element).
我可以正常播放背景音乐,但是当我尝试播放声音效果时,界面冻结了几秒钟(音乐一直在播放),然后当它解冻时,出现Windows错误声音
I am able to play background music with no problem, but when I try to play a sound effect, the interface freezes for a couple of seconds (the music keeps playing) and then when it unfreezes, I get the Windows error sound.
这就是我所拥有的(音乐具有相似的体系结构):
Here is what I have (the music has a similar architecture):
MySoundEffect.qml
MySoundEffect.qml
Loader {
id: container
source: "MobilitySoundEffect.qml"
property bool valid: item !== null
property real audioVolume: 1.0
property int loops: 1
function initialise() {
if(valid) {
item.volume = container.audioVolume
item.loops = container.loops
}
}
function play(filename) { if(valid) item.play(filename); }
function stop() { if(valid) item.stop(); }
onLoaded: container.initialise()
onAudioVolumeChanged: if(valid) item.volume = audioVolume;
}
MobilitySoundEffect.qml
MobilitySoundEffect.qml
Item {
id: container
property url source: ""
property real volume: 1.0
property int loops: 1
function play(filename) {
if (settings.isFxOn()) {
source = filename;
}
}
function stop() { soundEffect.stop(); }
SoundEffect {
id: soundEffect
source: container.source
volume: container.volume
loops: container.loops
onStatusChanged: if (Audio.Loaded == status) play();
}
}
这是我注册它们的方式(为简洁起见):
And here is how I register them (trimmed for brevity):
main.qml
Rectangle {
id: main
MyAudio {
id: music
}
MySoundEffect {
id: soundEffects
}
PageStack {
id: pageStack
Component.onCompleted: {
pageStack.push(Qt.resolvedUrl("pages/IntroPage.qml"));
}
}
}
最后,这就是我称呼他们播放音乐或声音效果的方式:
Finally, this is how I call them to play music or a sound effect:
soundEffects.play("file://assets/audio/click.ogg");
music.play("file://assets/audio/menu.mp3");
同样,音乐播放良好,声音效果不佳.而且,据我测试,它与文件类型无关.我无法播放WAV,MP3或播放音乐.
Again, music plays fine, sound effects don't. And, as far as I tested, it has nothing to do with the filetype. I couldn't get wav, mp3 or off to play.
我的设置是Windows 7,QtSDK 7.4.7,Symbian 1.1.
My setup is Windows 7, QtSDK 7.4.7, Symbian 1.1.
在Microsoft Visual Studio Compiler 9.0下,我得到了冻结和错误声音,但是在MinGW 4.4下,没有冻结或错误声音,但仍然没有声音效果.
Under Microsoft Visual Studio Compiler 9.0 I get the freezing and the error sound, but under MinGW 4.4, there is no freeze or error sound, yet, still no sound effect.
我现在将我的所有资产(qml,音频,txt等)移到了QResource文件中,现在既没有声音效果也没有音乐播放.每当我尝试播放音乐时,输出中都会显示以下内容:
I have now moved all my assets (qml, audio, txt, etc) into a QResource file and now neither sound effect nor music is playing. Whenever I try to play music I get the following in the output:
qrc文件:/audio/menu.ogg
尺寸353349
顺序0
qrc file :/audio/menu.ogg
Size 353349
Sequential 0
我真的需要解决此问题,所以我开始了赏金计划.请帮忙.
I really need to fix this, so I have started a bounty. Please help.
谢谢.
实际上,QSound无法播放来自资源的声音,并且我无法使SoundEffect元素起作用.因此,我的解决方法如下:
Indeed, QSound cannot play sounds from a resource and I cannot make the SoundEffect element to work. So my work around is the following:
我使用QML Audio元素播放背景音乐(支持淡入/淡出),并使用Phonon在C ++中创建了SoundEffects类来播放声音效果.
I use the QML Audio element to play background music (which support fade in/fade out) and I created a SoundEffects class in C++ using Phonon to play the sound effects.
似乎工作正常,所以我不再碰它.
It seems to work fine, so I'm not touching it again.
推荐答案
我认为QML Audio元素是QSound的包装,QSound无法访问Qt资源文件系统中的声音文件(在文档中已提及).请注意,QSound不支持资源.将来的Qt版本中可能会解决此问题..关于Windows警告/错误声音,我想,播放声音效果可能会阻塞主UI线程.
I think the QML Audio element is a wrapper around QSound, which cannot access sound files from Qt Resource File System (Mentioned in the Document)Note that QSound does not support resources. This might be fixed in a future Qt version.. Regarding the Windows warning/error sound, I guess, playing the sound effect may be blocking the main UI thread.
如果QSound无法同时播放两个音频文件,也可能是引起这种情况的原因.如果QSound类存在一些限制,则可以尝试 QtMultimedia或Phonon .
It may also be caused, if QSound cannot play two audio files simultaneously. You can try QtMultimedia or Phonon if there are some limitations in QSound class.
这篇关于QML:SoundEffect不起作用,音频起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!