Yesterday I saw a developer send a question asking how to play the sound. There are some reasons why the simulator cannot be tested. Actually, it's easy to play the sound. If you use Qmake, you may need to make some changes before you can play them correctly on your phone.
We first use the SDK to create a simple project (QML app with easy UI "Qmake"). We remember to revise our main.qml as follows:
Import QtQuick 2.0import ubuntu.components 1.1import qtmultimedia 5.0/*! \brief MainView with a Label and Button Elements.*/mainview {//ObjectName for functional testing purposes (autopilot- QT5) ObjectName: "MainView"//note! ApplicationName needs to match the "name" field of the click Manifest ApplicationName: "Audio.liu-xiao-guo"/* T The enables the application to change orientation when the device is rotated. The default is False. *///automaticorientation:true//Removes the old toolbar and enables new features of the new header. Usedeprecatedtoolbar:false width:units.gu (height:units.gu) page {title:i18n.tr ("Audio") SoundEffect {Id:bubblepop Source: "Bubblepop.wav" volume:1.0} MEDIAPL Ayer {id:backgroundmusic Source: "Background.mp3" Autoplay:true Loops:medi Aplayer.infinite volume:0.3 } Row {anchors.centerIn:parent button {text: "Play bubble" OnClicked: {bubblepop.play (); } } } }}
Here we use the Qtmultimedia library.
Import Qtmultimedia 5.0
We also use two different methods to play the sound. These two sounds can be mixed together when playing. At the same time, we should remember to do the following settings:
Add "audio" security policy. Since we use qmake, we need to modify our. Pro file to include our sound files in our application:
TEMPLATE = Auxtarget = audioresources + = audio.qrcqml_files + $ $files (*.qml,true) $ $files (*.js,true) $ $files ( Sounds/*.mp3,true) $ $files (sounds/*.wav,true) conf_files + = audio.apparmor audio.desktop Audio.pngother_files + = $${conf_files} $${qml_files} #specify where the Qml/js FILES are installed Toqml_files.path = /audioqml_files.files + = $${qml_files} #specify where the config files are installed Toconfig_files.path =/audioconfig_fi Les.files + = $${conf_files}installs+=config_files qml_files
So in our project, there will be our "sounds" in the Directory of sound files. They can be played in our QML files.
All the source code in: Git clone https://gitcafe.com/ubuntu/audio.git
How to play Music in the Ubuntu qml app