Play multiple mp3 files with QMediaPlayer
-
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() -
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()@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 usetime.sleep(2)and then change the source I imagine? Remove that and replace with aQTimer()for 2 seconds: in the slot attached to that change the source for the player. I am guessing that is what is required. -
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() -
M Mahmoud_213 has marked this topic as solved on