как подключить к нему слайдер громкости для работы с ним - PullRequest
0 голосов
/ 03 марта 2020
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtCore import Qt
from PyQt5.QtMultimedia import QMediaPlayer, QMediaContent, QMediaMetaData

from PyQt5.QtWidgets import QApplication, QWidget, QInputDialog, QLineEdit, QFileDialog, QSlider

class Ui_MainWinpage(object):
    def setupUi(self, MainWinpage):
        MainWinpage.setObjectName("MainWinpage")
        MainWinpage.resize(507, 301)
        self.centralwidget = QtWidgets.QWidget(MainWinpage)
        self.centralwidget.setAutoFillBackground(True)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")
        #slider
        self.horizontalSlider = QtWidgets.QSlider(self.centralwidget)
        self.horizontalSlider.setGeometry(QtCore.QRect(190, 180, 160, 16))
        self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal)
        self.horizontalSlider.setObjectName("horizontalSlider")
        self.horizontalSlider.valueChanged[int].connect(self.changeValue)
        self.gridLayout.addWidget(self.horizontalSlider, 0, 0, 1, 3)
        self.play_song = QtWidgets.QPushButton(self.centralwidget)
        self.play_song.setObjectName("play_song")
        self.gridLayout.addWidget(self.play_song, 1, 0, 1, 1)
        self.pause_song = QtWidgets.QPushButton(self.centralwidget)
        self.pause_song.setObjectName("pause_song")
        self.gridLayout.addWidget(self.pause_song, 1, 1, 1, 1)
        self.stop_song = QtWidgets.QPushButton(self.centralwidget)
        self.stop_song.setObjectName("stop_song")
        self.gridLayout.addWidget(self.stop_song, 1, 2, 1, 1)
        self.previous = QtWidgets.QPushButton(self.centralwidget)
        self.previous.setObjectName("previous")
        self.gridLayout.addWidget(self.previous, 2, 0, 1, 1)
        self.shuffle = QtWidgets.QPushButton(self.centralwidget)
        self.shuffle.setObjectName("shuffle")
        self.gridLayout.addWidget(self.shuffle, 2, 1, 1, 1)
        self.next = QtWidgets.QPushButton(self.centralwidget)
        self.next.setObjectName("next")
        self.gridLayout.addWidget(self.next, 2, 2, 1, 1)
        #for volume
        self.volumeSlider = QSlider(Qt.Horizontal)
        self.volumeSlider.setMaximum(32767)
        self.volumeSlider.setPageStep(1024)
        self.gridLayout.addWidget(self.volumeSlider, 4, 0, 1, 2)

        MainWinpage.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWinpage)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 507, 22))
        self.menubar.setObjectName("menubar")
        self.menufile = QtWidgets.QMenu(self.menubar)
        self.menufile.setObjectName("menufile")
        MainWinpage.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWinpage)
        self.statusbar.setObjectName("statusbar")
        MainWinpage.setStatusBar(self.statusbar)
        self.actionopen = QtWidgets.QAction(MainWinpage)
        self.actionopen.setObjectName("actionopen")
        self.menufile.addAction(self.actionopen)
        self.menubar.addAction(self.menufile.menuAction())

        self.pushButton = QtWidgets.QPushButton(MainWinpage)

        self.pushButton.setObjectName("pushButton")
        #for Qtmultimedia
        self.media_player = QMediaPlayer()
        self.media_player.durationChanged.connect(self.horizontalSlider.setMaximum)
        self.media_player.positionChanged.connect(self.horizontalSlider.setValue)
        self.volumeSlider.valueChanged.connect(self.changeVolume)
        self.retranslateUi(MainWinpage)
        QtCore.QMetaObject.connectSlotsByName(MainWinpage)

    def retranslateUi(self, MainWinpage):
        _translate = QtCore.QCoreApplication.translate
        MainWinpage.setWindowTitle(_translate("MainWinpage", "MainWindow"))
        self.play_song.setText(_translate("MainWinpage", "play"))
        self.pause_song.setText(_translate("MainWinpage", "pause"))
        self.stop_song.setText(_translate("MainWinpage", "stop"))
        self.previous.setText(_translate("MainWinpage", "previous"))
        self.shuffle.setText(_translate("MainWinpage", "shuffle"))
        self.next.setText(_translate("MainWinpage", "next"))
        self.menufile.setTitle(_translate("MainWinpage", "file"))
        self.actionopen.setText(_translate("MainWinpage", "open "))

        self.previous.setText(_translate("MainWinpage", "Browse"))
        self.previous.clicked.connect(self.pushButton_handler)
        self.play_song.clicked.connect(self.play_music)
        self.pause_song.clicked.connect(self.pause_music)
        self.stop_song.clicked.connect(self.stop_music)

    def changeValue(self, value):
        blocked = self.media_player.blockSignals(True)
        value = self.horizontalSlider.value()
        self.media_player.setPosition(value)
        self.media_player.blockSignals(blocked)

    def changeVolume(self, value):
        self.volume = value


    def pushButton_handler(self):
        print("Button pressed")
        self.open_dialog_box()

    #for opening that file which you want to open
    def open_dialog_box(self):
        filename = QFileDialog.getOpenFileName()
        path = str(filename[0])
        self.media_player.setMedia(QMediaContent(QtCore.QUrl.fromLocalFile(path)))
        print("path is ",path)
        self.globalpath = path
        print("global paths is ", self.globalpath)

    #for playing that file which we had choosen from
    def play_music(self):
        artist = self.media_player.metaData(QMediaMetaData.AlbumArtist)
        title = self.media_player.metaData(QMediaMetaData.Title)
        self.media_player.play()
        print("song name:",self.globalpath)
        print(f"Now playing: {title} by {artist}")

    #for pause that selected music
    def pause_music(self):
        self.media_player.pause()
        print("song is paused")

    #for stoping that music
    def stop_music(self):
        self.media_player.stop()
        print("song is stopped")


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWinpage = QtWidgets.QMainWindow()
    ui = Ui_MainWinpage()
    ui.setupUi(MainWinpage)
    MainWinpage.show()
    sys.exit(app.exec_())

Ответы [ 2 ]

1 голос
/ 03 марта 2020

Вместо использования pygame.mixer вы можете рассмотреть возможность использования PyQt5.QtMultimedia.QMediaPlayer. Преимущество этого заключается в том, что QMediaPlayer издает сигналы при изменении длительности и / или текущей позиции песни, которые можно использовать для обновления позиции и максимального значения ползунка. Метаданные песни (название, исполнитель и т. Д. c.) Доступны через QMediaPlayer.metaData, например,

from PyQt5.QtMultimedia import QMediaPlayer, QMediaContent, QMediaMetaData

class Ui_MainWinpage(object):
    def setupUi(self, MainWinpage):
        ....
        self.media_player = QMediaPlayer()
        self.media_player.durationChanged.connect(self.horizontalSlider.setMaximum)
        self.media_player.positionChanged.connect(self.horizontalSlider.setValue)

    def changeValue(self, value):
        blocked = self.media_player.blockSignals(True)
        value = self.horizontalSlider.value()
        self.media_player.setPosition(value)
        self.media_player.blockSignals(blocked)

    def open_dialog_box(self):
        filename = QFileDialog.getOpenFileName()
        path = str(filename[0])
        self.media_player.setMedia(QMediaContent(QtCore.QUrl.fromLocalFile(path)))
        print("path is ",path)
        self.globalpath = path

    def play_music(self):
        artist = self.media_player.metaData(QMediaMetaData.AlbumArtist)
        title = self.media_player.metaData(QMediaMetaData.Title)
        self.media_player.play()
        print(f"Now playing: {title} by {artist}")

    def pause_music(self):
        self.media_player.pause()
        print("song is paused")

    def stop_music(self):
        self.media_player.stop()
        print("song is stopped")
.
0 голосов
/ 03 марта 2020

Чтобы ползунок отображал положение воспроизводимой песни в реальном времени, используйте

Похоже, что нет API для определения продолжительности воспроизведения песни в микшере PyGame, что может стать дополнительным препятствием.

...