如何连接其中的音量滑块以使用它
how to connect volume slider in it to work with it
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_())
要让滑块显示正在播放的歌曲的实时位置,请使用
- PyQt 计时器,如在例如
- pygame.mixer.music.get_pos() 获取歌曲的当前位置(大致如此)
setValue
(可能 setRange
在滑块上。
似乎没有 API 来查明 PyGame 混音器当前播放歌曲的时长,因此这可能是一个额外的障碍。
您可以考虑使用 PyQt5.QtMultimedia.QMediaPlayer
而不是使用 pygame.mixer
。这样做的好处是 QMediaPlayer
会在持续时间 and/or 歌曲的当前位置发生变化时发出信号,您可以使用它来更新滑块的位置和最大值。可以通过 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")
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_())
要让滑块显示正在播放的歌曲的实时位置,请使用
- PyQt 计时器,如在例如
- pygame.mixer.music.get_pos() 获取歌曲的当前位置(大致如此)
setValue
(可能setRange
在滑块上。
似乎没有 API 来查明 PyGame 混音器当前播放歌曲的时长,因此这可能是一个额外的障碍。
您可以考虑使用 PyQt5.QtMultimedia.QMediaPlayer
而不是使用 pygame.mixer
。这样做的好处是 QMediaPlayer
会在持续时间 and/or 歌曲的当前位置发生变化时发出信号,您可以使用它来更新滑块的位置和最大值。可以通过 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")