问题
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).
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
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
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");
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.
My setup is Windows 7, QtSDK 7.4.7, Symbian 1.1.
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.
Update
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 file :/audio/menu.ogg
Size 353349
Sequential 0
I really need to fix this, so I have started a bounty. Please help.
Thank you in advance.
Solution (more like a work around)
Indeed, QSound cannot play sounds from a resource and I cannot make the SoundEffect element to work. So my work around is the following:
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.
回答1:
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.
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.
来源:https://stackoverflow.com/questions/8440600/qml-soundeffect-not-working-audio-does