Problem initializing audio player for automatic playback on launch
-
Hello everyone,
Thank you for the wonderful work everyone does here!
I'm trying to fix some issues I've encountered on two apps I'm developing (Radio Nova and UT Nextcloud Music) and that are already available on the store.The first problem is audio playback at first launch. It is currently necessary to manually click on pause then play for the audio to actually launch. It's as if the player hadn't been properly initialized.
Here's an example of Main.qml code that reproduces this problem.
I don't have any problems with clickable desktop. Unfortunately, I can't test on my smartphone other than with a build because clickable doesn't work properly (issue 447 of clickable), so I don't have the execution logs on smartphone.The second problem, perhaps linked to the system's audio management, is that the player does not skip to the next song if the application is not in focus.
In other words, everything's fine if the app is open on the screen, but if the player is put to sleep or another application is displayed, it doesn't skip to the next audio track.
If these problems are not related, I can open another topic.Can you help me, please?
import QtQuick 2.9 import Lomiri.Components 1.3 import QtQuick.Layouts 1.3 import Qt.labs.settings 1.0 import QtMultimedia 5.12 import QtQuick.Controls 2.2 import QtQuick.Controls.Material 2.1 import QtQuick.Controls.Universal 2.1 import QtSystemInfo 5.5 import Ubuntu.Components 1.3 as Ubuntu MainView { id: root objectName: 'mainView' applicationName: 'audio.gwado' automaticOrientation: true width: units.gu(45) height: units.gu(75) Audio { id: audioPlayer source: "https://novazz.ice.infomaniak.ch/novazz-128.mp3" } Page { anchors.fill: parent header: PageHeader { id: header title: i18n.tr('audio') } Column { anchors.centerIn: parent Button { id: playPauseButton text: audioPlayer.playbackState === Audio.PlayingState ? i18n.tr("Pause") : i18n.tr("Play") onClicked: { if (audioPlayer.playbackState === Audio.PlayingState) { audioPlayer.pause() } else { audioPlayer.play() } } } } } }
The imported libraries are certainly too numerous, contrary to my development skills.
-
@gwado The documentation (https://doc.qt.io/qt-5/qml-multimedia.html#audio) doesn't say that audio play starts automatically.
TryComponent.onCompleted: { this.play() }
Or setting
autoLoad: true
Always inside the audio component -
@ikoz Sorry, I misspoke.
I want the audio to play when I use this.play() (via a button, for example). It works except the first time.- this.play(): no playback
- this.pause() then this.play(): playback
- Other this.play() (for another song, for example): playback
I now display the audioplayer status.
On PC, it quickly goes from 2 (loading) to 6 (buffering).
On smartphone, it stalls at 2 (loading) until I do step 2 above, when it switches to 3 (loaded).
The behavior seems different. The player seems to be stuck at 2 (Loading).import QtQuick 2.9 import QtQuick.Controls 2.2 import QtMultimedia 5.12 ApplicationWindow { visible: true width: 400 height: 200 title: "Simple QML Audio Player" // Media Player element Audio { id: audioPlayer source: "../assets/audio.mp3" autoLoad: true } // Layout for controls Column { anchors.centerIn: parent spacing: 10 Text { text: audioPlayer.status + " - " + audioPlayer.position // Show status of the player and the position of track } // Play, Pause, Stop Buttons Row { spacing: 10 Button { text: "Play" onClicked: audioPlayer.play() } Button { text: "Pause" onClicked: audioPlayer.pause() } Button { text: "Stop" onClicked: audioPlayer.stop() } } } }
-
@gwado you could use the MediaPlayer element so it plays audio that would play with screen off and could get controlled by the audio indicator control buttons as well
-
@CiberSheep Thank you. I was using mediaplayer but the problem was the same. I'll dig up the subject again and go over the code. As my request is basic, the problem is certainly in my approach and my code.
I will be back. -
@gwado you can check Podbird Phoenix (even if the code is a bit old... Ubuntu->Lomiri... but you can see a working app
https://github.com/maciek134/podphoenix/blob/master/app/podphoenix.qml#L241
-
@CiberSheep Thank you for sharing this code with me.
I've noticed that the problem seems to exist for this app too, as the developer uses a timer to start playback with a delay.
I applied this in my code and it seems to work. I'll now be able to add a few features to it.
Thanks again.Here's a code snippet if it helps anyone in the future. Timer interval (1500ms) to be adapted.
Timer { id: playStarter interval: 1500 onTriggered: { musicPlayer.play() } } MediaPlayer { id: musicPlayer } function pause() { musicPlayer.pause(); } function play() { if (musicPlayer.source) { musicPlayer.pause(); playStarter.restart(); } }