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

Problem initializing audio player for automatic playback on launch

Scheduled Pinned Locked Moved App Development
7 Posts 3 Posters 332 Views 2 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.
    • G Offline
      gwado
      last edited by 3 Nov 2024, 14:33

      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. 🙂

      I 1 Reply Last reply 3 Nov 2024, 15:08 Reply Quote 0
      • I Offline
        ikoz @gwado
        last edited by ikoz 11 Mar 2024, 15:10 3 Nov 2024, 15:08

        @gwado The documentation (https://doc.qt.io/qt-5/qml-multimedia.html#audio) doesn't say that audio play starts automatically.
        Try

        Component.onCompleted: {
            this.play()
        }
        

        Or setting autoLoad: true
        Always inside the audio component

        May the source be with you

        G 1 Reply Last reply 3 Nov 2024, 15:43 Reply Quote 0
        • G Offline
          gwado @ikoz
          last edited by gwado 11 Mar 2024, 16:24 3 Nov 2024, 15:43

          @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.

          1. this.play(): no playback
          2. this.pause() then this.play(): playback
          3. 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()
                      }
                  }
          
              }
          
          }
          
          
          C 1 Reply Last reply 3 Nov 2024, 21:55 Reply Quote 0
          • C Offline
            CiberSheep @gwado
            last edited by 3 Nov 2024, 21:55

            @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

            https://ubports.gitlab.io/docs/api-docs/index.html?p=qtmultimedia%2Fqml-qtmultimedia-mediaplayer.html

            Another planet, another time, another universe!

            G 1 Reply Last reply 4 Nov 2024, 20:15 Reply Quote 1
            • G Offline
              gwado @CiberSheep
              last edited by 4 Nov 2024, 20:15

              @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.

              C 1 Reply Last reply 4 Nov 2024, 23:09 Reply Quote 1
              • C Offline
                CiberSheep @gwado
                last edited by 4 Nov 2024, 23:09

                @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

                Another planet, another time, another universe!

                G 1 Reply Last reply 5 Nov 2024, 22:14 Reply Quote 0
                • G Offline
                  gwado @CiberSheep
                  last edited by 5 Nov 2024, 22:14

                  @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();
                          }
                      }
                  
                  
                  
                  1 Reply Last reply Reply Quote 2
                  5 out of 7
                  • First post
                    5/7
                    Last post