Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Play multiple mp3 files with QMediaPlayer
Qt 6.11 is out! See what's new in the release blog

Play multiple mp3 files with QMediaPlayer

Scheduled Pinned Locked Moved Solved Qt for Python
3 Posts 2 Posters 195 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.
  • M Offline
    M Offline
    Mahmoud_213
    wrote on last edited by
    #1

    Hi everybody,
    I am trying to develop a software that can play sound from mp3 files, I am trying to do this with QMediaPlayer but it only plays the last file that i give him, here is my code:

    self.player=QMediaPlayer()
    self.audioOutput = QAudioOutput()
    
    self.player.setAudioOutput(self.audioOutput)
    
    self.player.setSource(QUrl.fromLocalFile(“sound/001000.mp3”))
    
    self.player.play()
    
    while self.player.isPlaying():
    
    time.sleep(2)
    
    self.player.setSource(QUrl.fromLocalFile(“sound/001001.mp3”))
    
    self.player.play()
    
    JonBJ 1 Reply Last reply
    0
    • M Mahmoud_213

      Hi everybody,
      I am trying to develop a software that can play sound from mp3 files, I am trying to do this with QMediaPlayer but it only plays the last file that i give him, here is my code:

      self.player=QMediaPlayer()
      self.audioOutput = QAudioOutput()
      
      self.player.setAudioOutput(self.audioOutput)
      
      self.player.setSource(QUrl.fromLocalFile(“sound/001000.mp3”))
      
      self.player.play()
      
      while self.player.isPlaying():
      
      time.sleep(2)
      
      self.player.setSource(QUrl.fromLocalFile(“sound/001001.mp3”))
      
      self.player.play()
      
      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #2

      @Mahmoud_213
      I know nothing about this, but by the sound of it the Qt event loop needs to be running for stuff to play/sound? So you cannot use time.sleep(2) and then change the source I imagine? Remove that and replace with a QTimer() for 2 seconds: in the slot attached to that change the source for the player. I am guessing that is what is required.

      1 Reply Last reply
      2
      • M Offline
        M Offline
        Mahmoud_213
        wrote on last edited by Mahmoud_213
        #3

        I found a solution, it involves in connecting the signal "playingChanged" of QMediaPlayer to a function that plays the next track.

        self.player=QMediaPlayer()
        self.audioOutput = QAudioOutput()
        self.player.setAudioOutput(self.audioOutput)
        self.player.playingChanged.connect(self.play_next)
        
        def play_next(self):
            if self.player.playbackState()!=QMediaPlayer.PlaybackState.PlayingState:
                self.player.setSource(QUrl.fromLocalFile("next_track.mp3")) 
                self.player.play()
        
        1 Reply Last reply
        0
        • M Mahmoud_213 has marked this topic as solved on

        • Login

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