0

I'm using PyQt5 to make a music player. I wanna know how to move QSlider over time.

I wrote some code but when i run it the program crashes. I wanna know how to implement it.

I searched for this in google and read the documentation but I didn't find anything.

This is some of my code:


from pygame import mixer
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from mutagen.mp3 import MP3
import sys
import time


mixer.init()

file = ""
playing = False
Volume_value = 100
Song_time = 0
Song_current_time = 0



class Ui_mainWindow(object):
    def setupUi(self, mainWindow):
        global Volume_value
        global Song_time

        self.SongProgress = QtWidgets.QSlider(self.centralwidget)
        self.SongProgress.setGeometry(QtCore.QRect(299, 130, 241, 20))
        self.SongProgress.setOrientation(QtCore.Qt.Horizontal)
        self.SongProgress.setObjectName("SongProgress")

        if playing == True:
            self.Get_current_time()


    def Get_length_of_file(self):
        global file
        global Song_time

        audio = MP3(file[0])
        Song_time = audio.info.length

        #  Sets the Songs time to SongProgress
        self.SongProgress.setMaximum(int(Song_time))


    def Get_current_time(self):
        global Song_current_time
        global playing

        t = int(Song_current_time)

        if playing:
            for i in range(t):
                self.SongProgress.setValue(i)
                time.sleep(1)

if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    mainWindow = QtWidgets.QMainWindow()
    ui = Ui_mainWindow()
    ui.setupUi(mainWindow)

    mainWindow.show()
    sys.exit(app.exec_())

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • With which library are you playing the audio? mutagen only serves to obtain the metadata of the audio. Your question is unclear. – eyllanesc Jan 02 '22 at 15:22
  • 1
    Don't use pygame - use [QMediaPlayer](https://doc.qt.io/qt-5/qmediaplayer.html), so you can connect to signals like [positionChanged](https://doc.qt.io/qt-5/qmediaplayer.html#positionChanged). – ekhumoro Jan 02 '22 at 17:46

0 Answers0