Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to play music in a function until the end of the music to end the function ?
Forum Updated to NodeBB v4.3 + New Features

How to play music in a function until the end of the music to end the function ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 7 Posters 1.1k Views 3 Watching
  • 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.
  • P plalq

    As in the title,in linux, what can i do ?

    Pl45m4P Offline
    Pl45m4P Offline
    Pl45m4
    wrote on last edited by
    #3

    @plalq

    QSound::play

    https://doc.qt.io/qt-5/qsound.html#play-1

    to the end of the function

    You cant control that. play will start its own loop and return immediately to the function where it was called. So if your music file is 3mins long, the music will still play, even if the function is not executed at that moment.

    You could use QSoundEffect and mute or stop the music when you reached the end of your function.

    https://doc.qt.io/qt-5/qsoundeffect.html


    If debugging is the process of removing software bugs, then programming must be the process of putting them in.

    ~E. W. Dijkstra

    artwawA 1 Reply Last reply
    0
    • Pl45m4P Pl45m4

      @plalq

      QSound::play

      https://doc.qt.io/qt-5/qsound.html#play-1

      to the end of the function

      You cant control that. play will start its own loop and return immediately to the function where it was called. So if your music file is 3mins long, the music will still play, even if the function is not executed at that moment.

      You could use QSoundEffect and mute or stop the music when you reached the end of your function.

      https://doc.qt.io/qt-5/qsoundeffect.html

      artwawA Offline
      artwawA Offline
      artwaw
      wrote on last edited by
      #4

      @Pl45m4
      QMultimediaPlayer::start() at the start of the method and QMultimediaPlayer::stop() at the end, thank you.

      Mentioned by you QSound also has stop() method.

      For more information please re-read.

      Kind Regards,
      Artur

      Pl45m4P 1 Reply Last reply
      1
      • artwawA artwaw

        @Pl45m4
        QMultimediaPlayer::start() at the start of the method and QMultimediaPlayer::stop() at the end, thank you.

        Mentioned by you QSound also has stop() method.

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by Pl45m4
        #5

        @artwaw

        But none of them ensures that the sound file reached its end, when you call stop at the end of your function where play was called :)

        @plalq has to either check with isFinished() or deal with it


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        artwawA 1 Reply Last reply
        1
        • Pl45m4P Pl45m4

          @artwaw

          But none of them ensures that the sound file reached its end, when you call stop at the end of your function where play was called :)

          @plalq has to either check with isFinished() or deal with it

          artwawA Offline
          artwawA Offline
          artwaw
          wrote on last edited by
          #6

          @Pl45m4 well, yes. Possibly I misread the title... Signal/slot relation being a backbone of Qt - I thought it's quite obvious.

          For more information please re-read.

          Kind Regards,
          Artur

          1 Reply Last reply
          0
          • P Offline
            P Offline
            plalq
            wrote on last edited by
            #7

            Thank you both for your replies, I didn't express myself clearly, my mistake. I want to open a new thread for playing music, and when the music is finished, I can close the thread. The only way I can think of is to use QTimer to set a timer that counts down to the same amount of time as the music, and then listen to the timeout slot function to exit the thread. Is there a class that has a finished signal when the music finishes playing to do this?@artwaw @Pl45m4

            1 Reply Last reply
            0
            • B Offline
              B Offline
              Bonnie
              wrote on last edited by
              #8

              The playing part is asynchronous. There's no need to open a thread.

              P 1 Reply Last reply
              3
              • B Bonnie

                The playing part is asynchronous. There's no need to open a thread.

                P Offline
                P Offline
                plalq
                wrote on last edited by
                #9

                @Bonnie ohhhh! thx, i found QSoundEffect::playingChanged is what i need , but it is not right now.

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  Kelvin Saunders
                  Banned
                  wrote on last edited by
                  #10
                  This post is deleted!
                  1 Reply Last reply
                  0
                  • H Offline
                    H Offline
                    hellobaby
                    wrote on last edited by
                    #11

                    This is also what I was looking for for my device. Music playback is currently a trend of users of smart devices.

                    https://radiofrench.fr/

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      AndrewKenneth
                      wrote on last edited by
                      #12
                      This post is deleted!
                      1 Reply Last reply
                      0
                      • J.HilkJ J.Hilk locked this topic on

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved