如何连接其中的音量滑块以使用它

Posted

技术标签:

【中文标题】如何连接其中的音量滑块以使用它【英文标题】:how to connect volume slider in it to work with it 【发布时间】:2020-03-03 12:15:57 【问题描述】:
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_())

【问题讨论】:

请说得更具体一些,准确解释你想要达到的目标,因为不清楚。 【参考方案1】:

您可以考虑使用PyQt5.QtMultimedia.QMediaPlayer,而不是使用pygame.mixer。这样做的好处是QMediaPlayer 在歌曲的持续时间和/或当前位置发生变化时发出信号,您可以使用它来更新滑块的位置和最大值。歌曲的元数据(标题、艺术家等)可以通过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")

【讨论】:

非常感谢 Heike 我将使用 media_player ...谢谢【参考方案2】:

要让滑块显示正在播放的歌曲的实时位置,请使用

PyQt 计时器,如在例如How to create a timer in pyqt pygame.mixer.music.get_pos() 获取歌曲的当前位置(大约无论如何) setValue(滑块上可能还有 setRange

似乎没有 API 可以确定 PyGame 混音器当前播放歌曲的时长,因此这可能是一个额外的障碍。

【讨论】:

以上是关于如何连接其中的音量滑块以使用它的主要内容,如果未能解决你的问题,请参考以下文章

如何添加滑块以在视图中向下移动? [关闭]

按下音量按钮时更改音量滑块位置

当 <ul> 中只有 1 个 <li> 时,如何更改任何内容滑块以禁用下一个/上一个按钮?

如何自定义滑块控件,如 wpf 中的音量

如何获得光滑的滑块以在循环中平滑过渡

如何设置滑块以固定位置?