• Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Register
  • Login
UBports Robot Logo UBports Forum
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Register
  • Login

How to start an audio stream at application start?

Scheduled Pinned Locked Moved Unsolved Support
5 Posts 2 Posters 244 Views 1 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • J Offline
      janmv
      last edited by 5 Feb 2025, 21:24

      I have made a small QML application with a MediaPlayer with a fixed URL stream source, which I want to start at application startup.
      Using clickabe 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.
      Using clickable 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 calls audioPlayer.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";
                  }
              }
          
          }
      
      
      K 1 Reply Last reply 6 Feb 2025, 01:56 Reply Quote 0
      • K Offline
        kugiigi @janmv
        last edited by 6 Feb 2025, 01:56

        @janmv Can you try starting the playback in Component.onCompleted?

        1 Reply Last reply Reply Quote 0
        • J Offline
          janmv
          last edited by 6 Feb 2025, 17:53

          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 with Qt.application.onStateChanged, to see if the application needs to be active, but no difference.

          1 Reply Last reply Reply Quote 0
          • K Offline
            kugiigi
            last edited by 7 Feb 2025, 03:12

            Perhaps MediaPlayer has a status that tells you it's ready to play. From the code you gave, you play on status changed but don't actually check which status it will play.

            1 Reply Last reply Reply Quote 0
            • J Offline
              janmv
              last edited by 7 Feb 2025, 20:19

              I would think the statuses have a meaning, but I was not able to find documentation for that. So I'm calling play() for whatever status shows up 🙂
              From what I tested through clickable desktop I found that Loaded indicates the player is ready to play, and apparently if the status is Loading, it is not. But on the phone no other statuses occur, I tested repeatedly and in a couple of cases I waited for at least 10 minutes for other statuses to show up. No luck.

              1 Reply Last reply Reply Quote 0
              2 out of 5
              • First post
                2/5
                Last post