QML: SoundEffect does not work, Audio does

I have two custom QML elements for audio, one for playing music (and an extension for the QML Audio element), and one for playing sound effects (which extends the QML SoundEffect element).

I can play background music without problems, but when I try to play the sound effect, the interface freezes for a couple of seconds (the music continues to play), and then when it defrosts, I get a sound with a Windows error.

Here's what I have (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 (in short 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, so I call them to play music or sound effect:

 soundEffects.play("file://assets/audio/click.ogg"); music.play("file://assets/audio/menu.mp3"); 

Again, the music plays perfectly, the sound effects do not. And, as far as I tested, this has nothing to do with the file type. I could not get wav, mp3 or turn off to play.

My setup is Windows 7, QtSDK 7.4.7, Symbian 1.1.

In Microsoft Visual Studio Compiler 9.0 I get a freezing sound and errors, but in MinGW 4.4 there is no freezing sound or error, but there is still no sound effect.

Update

Now I transferred all my assets (qml, audio, txt, etc.) to the QResource file, and now neither sound nor music plays. 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 started the bounty. Please, help.

Thanks in advance.

Solution (more like work)

In fact, QSound cannot play sounds from the resource, and I cannot get the SoundEffect element to work. Therefore, my work is as follows:

I use the QML Audio element to play background music (supporting fade in / fade out), and I created the SoundEffects class in C ++ using Phonon to play sound effects.

Everything seems to be fine, so I no longer touch it.

+4
source share
1 answer

I think the QML Audio element is a wrapper around QSound that cannot access audio files from the Qt Resource File System (mentioned in the document) Note that QSound does not support resources. This may be fixed in a future version of Qt. . Regarding the sound of Windows warning / error, I think that playing a sound effect can block the main stream of the user interface.

This can also be caused if QSound cannot play two audio files at the same time. You can try QtMultimedia or Phonon if there are some limitations in the QSound class.

+3
source

Source: https://habr.com/ru/post/1385461/


All Articles