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/stop audio file(eg. mp3 file) using multi-threads?

how to play/stop audio file(eg. mp3 file) using multi-threads?

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 5 Posters 1.5k Views
  • 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.
  • O Offline
    O Offline
    opengpu
    wrote on last edited by
    #1

    how to play/stop audio file(eg. mp3 file) using multi-threads?
    is QMediaPlayer thread-safe?
    what i want is: open/play a mp3 in one Thread; while pause/stop/close the mp3 file in the other Thread. is there any good way to do this?
    thank you!

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Why do you want to use it in a thread at all?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • O Offline
        O Offline
        opengpu
        wrote on last edited by
        #3

        yep. i need to play it in a callback which is not mainThread.
        and while playing the mp3, i want to pause/stop the mp3 in mainThread by press a button.

        1 Reply Last reply
        0
        • Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @opengpu said in how to play/stop audio file(eg. mp3 file) using multi-threads?:

          i need to play it in a callback

          There is no reason for this to play an audio file in a separate thread since it's async...

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          2
          • O Offline
            O Offline
            opengpu
            wrote on last edited by
            #5

            Play & Stop event may trigger at the same time from different threads.....

            1 Reply Last reply
            0
            • O Offline
              O Offline
              opengpu
              wrote on last edited by
              #6

              so should i open a new thread for QMediaPlayer. and callback-thread & mainThread both send msg to the mediaPlayer thread?? and i just use the reference/pointer of the QMediaPlayer(means the QMeidaPlayer is thread-safe & can be used at different threads at the same time)?

              jsulmJ 1 Reply Last reply
              0
              • O opengpu

                so should i open a new thread for QMediaPlayer. and callback-thread & mainThread both send msg to the mediaPlayer thread?? and i just use the reference/pointer of the QMediaPlayer(means the QMeidaPlayer is thread-safe & can be used at different threads at the same time)?

                jsulmJ Online
                jsulmJ Online
                jsulm
                Lifetime Qt Champion
                wrote on last edited by jsulm
                #7

                @opengpu said in how to play/stop audio file(eg. mp3 file) using multi-threads?:

                so should i open a new thread for QMediaPlayer

                No. As @Christian-Ehrlicher already said playing media files is ASYNCH already. Playing media files does not block your main thread -> no need for any threads, don't overengeneer the task. To pause/stop simply call pause()/stop()...

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                O 1 Reply Last reply
                2
                • jsulmJ jsulm

                  @opengpu said in how to play/stop audio file(eg. mp3 file) using multi-threads?:

                  so should i open a new thread for QMediaPlayer

                  No. As @Christian-Ehrlicher already said playing media files is ASYNCH already. Playing media files does not block your main thread -> no need for any threads, don't overengeneer the task. To pause/stop simply call pause()/stop()...

                  O Offline
                  O Offline
                  opengpu
                  wrote on last edited by
                  #8

                  @jsulm ok. so you mean i can just play/pause/stop in both mainThread and the other thread?
                  i am worried about : sometimes at the same time, stop() is trigered in mainThread & play() is triggered in the other thread...

                  jsulmJ 1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    kuzulis
                    Qt Champions 2020
                    wrote on last edited by
                    #9

                    @opengpu said in how to play/stop audio file(eg. mp3 file) using multi-threads?:

                    is QMediaPlayer thread-safe?

                    No.

                    1 Reply Last reply
                    0
                    • O opengpu

                      @jsulm ok. so you mean i can just play/pause/stop in both mainThread and the other thread?
                      i am worried about : sometimes at the same time, stop() is trigered in mainThread & play() is triggered in the other thread...

                      jsulmJ Online
                      jsulmJ Online
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @opengpu Why do you have two threads? If you really need two threads then simply send signals for pause/stop from the other thread and pause/stop in the main thread (where your QMediaPlayer is)...

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      O 1 Reply Last reply
                      0
                      • jsulmJ jsulm

                        @opengpu Why do you have two threads? If you really need two threads then simply send signals for pause/stop from the other thread and pause/stop in the main thread (where your QMediaPlayer is)...

                        O Offline
                        O Offline
                        opengpu
                        wrote on last edited by
                        #11

                        @jsulm i now have 2 threads, one is mainThread, and the other thread belong to some Callback func by some lib.
                        and play/stop can be both happen on these 2 thread...

                        1 Reply Last reply
                        0
                        • Christian EhrlicherC Online
                          Christian EhrlicherC Online
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          So why don't you simply do what @jsulm suggest and use signals/slots?

                          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                          Visit the Qt Academy at https://academy.qt.io/catalog

                          1 Reply Last reply
                          0
                          • O Offline
                            O Offline
                            opengpu
                            wrote on last edited by
                            #13

                            @opengpu said in how to play/stop audio file(eg. mp3 file) using multi-threads?:

                            QMediaPlayer

                            how to play .mp3 file in Windows. the filePath is C:\test\aaa.mp3

                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              Already answered here.

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              0

                              • Login

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