How to start an audio stream at application start?
-
I have made a small QML application with a MediaPlayer with a fixed URL stream source, which I want to start at application startup.
Usingclickabe desktop
the application works. The players status changes from 'Loading' to 'Loaded', and at this status change play() starts the player. My desktop is Ubuntu 20.04.
Usingclickable
the application does not work.clickable log
shows that the player status remains 'Loading', calling play() only puts the player in playback state 'paused'. My phone is a Fairphone 4, UT 20.04 OTA-7. The apparmor has"policy_groups": ["audio", "networking"],
.
For the code see below.
The button is to test for possible permission problems. But that is not the case, if clicked, the stream plays.
What also works is to start a timer (interval 3s) which callsaudioPlayer.play()
.
But that shouldn't be necessary, should it? What am I missing here?The code:
import QtQuick 2.7 import Lomiri.Components 1.3 import QtMultimedia 5.12 MainView { id: root Rectangle { anchors.fill: parent Button { id: manualPlay text: "Play" onClicked: { console.log("Clicked Play") audioPlayer.play() } } } MediaPlayer { id: audioPlayer audioRole: MediaPlayer.MusicRole source: "http://icecast.omroep.nl/radio4-eigentijdsfb-mp3" onStatusChanged: { console.log("onStatusChanged(): status = ", getPlayerStatus(status), " playbackState = ", getPlayerState(playbackState), " app state = ", getApplicationState(Qt.application.state)); if (audioPlayer.playbackState !== MediaPlayer.PlayingState) { console.log(" calling play()"); audioPlayer.play(); } } } function getPlayerStatus(playerStatusCode) { switch (playerStatusCode) { case MediaPlayer.NoMedia: return "NoMedia" case MediaPlayer.Loading: return "Loading" case MediaPlayer.Loaded: return "Loaded" case MediaPlayer.Buffering: return "Buffering" case MediaPlayer.Stalled: return "Stalled" case MediaPlayer.Buffered: return "Buffered" case MediaPlayer.EndOfMedia: return "End of media" case MediaPlayer.InvalidMedia: return "Invalid media" case MediaPlayer.UnknownStatus: return "Unknown status" default: return "unknown player status"; } } function getPlayerState(playerStateCode) { switch (playerStateCode) { case MediaPlayer.PlayingState: return "Playing" case MediaPlayer.StoppedState: return "Stopped" case MediaPlayer.PausedState: return "Paused" default: return "unknown player state"; } } function getApplicationState(applicationStateCode) { switch (applicationStateCode) { case Qt.ApplicationSuspended: return "Suspended" case Qt.ApplicationHidden: return "Hidden" case Qt.ApplicationInactive: return "Inactive" case Qt.ApplicationActive: return "Active" default: return "unknown application state"; } } }
-
@janmv Can you try starting the playback in
Component.onCompleted
? -
Thanks for the suggestion, but no, that does not work either. The player status and playback state is 'Loading' & "paused" before
onCompleted
triggers, and calling play() then doesn't change that.
I tried also withQt.application.onStateChanged
, to see if the application needs to be active, but no difference.