按照自己思路简单写了个桌面播放器,只有自己喜欢的歌。使用QtDesigner设计的ui界面,功能简单,还有很多bug@~@,代码奉上:
music_button.ui使用扩展工具pyuic5生成了music_button.py文件,内容如下:
# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(265, 65)
MainWindow.setMinimumSize(QtCore.QSize(265, 65))
MainWindow.setMaximumSize(QtCore.QSize(265, 169))
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(60, 10, 24, 24))
self.pushButton.setAutoFillBackground(False)
self.pushButton.setStyleSheet("border-image: url(:/qrc_files/up.png);")
self.pushButton.setText("")
self.pushButton.setAutoDefault(False)
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(110, 10, 24, 24))
self.pushButton_2.setStyleSheet("border-image: url(:/qrc_files/play.png);")
self.pushButton_2.setText("")
self.pushButton_2.status = 0
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_3.setGeometry(QtCore.QRect(160, 10, 24, 24))
self.pushButton_3.setStyleSheet("border-image: url(:/qrc_files/down.png);")
self.pushButton_3.setText("")
self.pushButton_3.setObjectName("pushButton_3")
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 265, 23))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.retranslateUi(MainWindow)
self.pushButton.clicked.connect(self.pushButton\_1\_click)
self.pushButton\_2.clicked.connect(self.pushButton\_2\_click)
self.pushButton\_3.clicked.connect(self.pushButton\_3\_click)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
\_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(\_translate("MainWindow", "MainWindow"))
def pushButton\_1\_click(self):
pass
def pushButton\_2\_click(self):
pass
def pushButton\_3\_click(self):
pass
from qrc_files import down
from qrc_files import play
from qrc_files import up
上面引入qrc文件,需要自己使用扩展工具pyrcc将图片资源转为python文件再引入,qrc文件内容如下:
以下是down.png生成的down.py文件:
# -*- coding: utf-8 -*-
from PyQt5 import QtCore
qt_resource_data = b"\
\x00\x00\x03\x2d\
\x89\
\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52\x00\
\x00\x00\x18\x00\x00\x00\x18\x08\x04\x00\x00\x00\x4a\x7e\xf5\x73\
\x00\x00\x00\x04\x67\x41\x4d\x41\x00\x00\xb1\x8f\x0b\xfc\x61\x05\
\x00\x00\x00\x20\x63\x48\x52\x4d\x00\x00\x7a\x26\x00\x00\x80\x84\
\x00\x00\xfa\x00\x00\x00\x80\xe8\x00\x00\x75\x30\x00\x00\xea\x60\
\x00\x00\x3a\x98\x00\x00\x17\x70\x9c\xba\x51\x3c\x00\x00\x00\x02\
\x62\x4b\x47\x44\x00\xff\x87\x8f\xcc\xbf\x00\x00\x00\x07\x74\x49\
\x4d\x45\x07\xe3\x01\x09\x02\x3b\x32\xeb\x8d\xcf\x60\x00\x00\x00\
\xab\x49\x44\x41\x54\x38\xcb\xbd\x92\x3b\x0e\xc2\x30\x10\x44\x9f\
\x62\xd9\x34\x20\x14\x72\x03\x0a\xae\x80\xb8\x83\xa1\x0b\x29\xe0\
\x0a\xb9\x0e\x35\xc7\xa2\x49\xa2\x54\x34\x54\x81\x16\x41\x81\x10\
\x8a\xfc\x61\x25\x14\xa6\xdb\xf1\xbc\xdd\x29\x0c\x3f\x48\x91\x91\
\x7c\x0f\x7d\xa4\xc9\xc9\xa8\xe5\xc0\x9d\x0b\x16\x13\x47\x54\x6f\
\xea\xa8\xb1\x18\x1a\x29\x00\x1d\x15\x1b\x74\xf8\x8a\x72\x9c\x2b\
\x15\xeb\x70\x31\xe5\xf1\xde\x48\x23\x05\xa2\x57\xfc\x40\x04\x09\
\x01\x2f\xc4\x62\x68\x79\x20\xd6\x9c\x03\x8b\xbe\x15\xfb\x0a\x9a\
\x15\x27\xce\xd2\xed\x9a\x1d\x25\x13\x79\x7c\x4f\xc9\xd8\x7d\x48\
\x02\xf1\x82\x19\x47\x6e\xff\x2d\xe3\xaf\xa4\xd9\x92\x0e\x56\x46\
\x91\xc7\xca\xb8\x1a\xb1\x64\x2a\x8f\x0f\xa5\x27\x47\x75\x26\xe3\
\xb4\x71\x5d\x6c\x00\x00\x00\x25\x74\x45\x58\x74\x64\x61\x74\x65\
\x3a\x63\x72\x65\x61\x74\x65\x00\x32\x30\x31\x39\x2d\x30\x31\x2d\
\x30\x39\x54\x30\x32\x3a\x35\x39\x3a\x35\x30\x2b\x30\x38\x3a\x30\
\x30\xbe\xc1\x87\xc1\x00\x00\x00\x25\x74\x45\x58\x74\x64\x61\x74\
\x65\x3a\x6d\x6f\x64\x69\x66\x79\x00\x32\x30\x31\x39\x2d\x30\x31\
\x2d\x30\x39\x54\x30\x32\x3a\x35\x39\x3a\x35\x30\x2b\x30\x38\x3a\
\x30\x30\xcf\x9c\x3f\x7d\x00\x00\x00\x43\x74\x45\x58\x74\x73\x6f\
\x66\x74\x77\x61\x72\x65\x00\x2f\x75\x73\x72\x2f\x6c\x6f\x63\x61\
\x6c\x2f\x69\x6d\x61\x67\x65\x6d\x61\x67\x69\x63\x6b\x2f\x73\x68\
\x61\x72\x65\x2f\x64\x6f\x63\x2f\x49\x6d\x61\x67\x65\x4d\x61\x67\
\x69\x63\x6b\x2d\x37\x2f\x2f\x69\x6e\x64\x65\x78\x2e\x68\x74\x6d\
\x6c\xbd\xb5\x79\x0a\x00\x00\x00\x18\x74\x45\x58\x74\x54\x68\x75\
\x6d\x62\x3a\x3a\x44\x6f\x63\x75\x6d\x65\x6e\x74\x3a\x3a\x50\x61\
\x67\x65\x73\x00\x31\xa7\xff\xbb\x2f\x00\x00\x00\x17\x74\x45\x58\
\x74\x54\x68\x75\x6d\x62\x3a\x3a\x49\x6d\x61\x67\x65\x3a\x3a\x48\
\x65\x69\x67\x68\x74\x00\x34\x30\x89\xbb\x0f\x1f\x00\x00\x00\x16\
\x74\x45\x58\x74\x54\x68\x75\x6d\x62\x3a\x3a\x49\x6d\x61\x67\x65\
\x3a\x3a\x57\x69\x64\x74\x68\x00\x34\x30\x71\x14\xcf\x92\x00\x00\
\x00\x19\x74\x45\x58\x74\x54\x68\x75\x6d\x62\x3a\x3a\x4d\x69\x6d\
\x65\x74\x79\x70\x65\x00\x69\x6d\x61\x67\x65\x2f\x70\x6e\x67\x3f\
\xb2\x56\x4e\x00\x00\x00\x17\x74\x45\x58\x74\x54\x68\x75\x6d\x62\
\x3a\x3a\x4d\x54\x69\x6d\x65\x00\x31\x35\x34\x36\x39\x37\x33\x39\
\x39\x30\x85\x91\xda\x96\x00\x00\x00\x10\x74\x45\x58\x74\x54\x68\
\x75\x6d\x62\x3a\x3a\x53\x69\x7a\x65\x00\x31\x38\x36\x42\x49\x71\
\xf5\x4d\x00\x00\x00\x62\x74\x45\x58\x74\x54\x68\x75\x6d\x62\x3a\
\x3a\x55\x52\x49\x00\x66\x69\x6c\x65\x3a\x2f\x2f\x2f\x68\x6f\x6d\
\x65\x2f\x77\x77\x77\x72\x6f\x6f\x74\x2f\x6e\x65\x77\x73\x69\x74\
\x65\x2f\x77\x77\x77\x2e\x65\x61\x73\x79\x69\x63\x6f\x6e\x2e\x6e\
\x65\x74\x2f\x63\x64\x6e\x2d\x69\x6d\x67\x2e\x65\x61\x73\x79\x69\
\x63\x6f\x6e\x2e\x63\x6e\x2f\x66\x69\x6c\x65\x73\x2f\x31\x31\x33\
\x2f\x31\x31\x33\x37\x30\x33\x38\x2e\x70\x6e\x67\xa4\xb3\xeb\x9e\
\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60\x82\
"
qt_resource_name = b"\
\x00\x09\
\x09\x5c\x33\xc3\
\x00\x71\
\x00\x72\x00\x63\x00\x5f\x00\x66\x00\x69\x00\x6c\x00\x65\x00\x73\
\x00\x08\
\x06\xe1\x5a\x27\
\x00\x64\
\x00\x6f\x00\x77\x00\x6e\x00\x2e\x00\x70\x00\x6e\x00\x67\
"
qt_resource_struct_v1 = b"\
\x00\x00\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x01\
\x00\x00\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x02\
\x00\x00\x00\x18\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\
"
qt_resource_struct_v2 = b"\
\x00\x00\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x01\
\x00\x00\x00\x00\x00\x00\x00\x00\
\x00\x00\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x02\
\x00\x00\x00\x00\x00\x00\x00\x00\
\x00\x00\x00\x18\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\
\x00\x00\x01\x6b\xb0\xb6\x90\x86\
"
qt_version = [int(v) for v in QtCore.qVersion().split('.')]
if qt_version < [5, 8, 0]:
rcc_version = 1
qt_resource_struct = qt_resource_struct_v1
else:
rcc_version = 2
qt_resource_struct = qt_resource_struct_v2
def qInitResources():
QtCore.qRegisterResourceData(rcc_version, qt_resource_struct, qt_resource_name, qt_resource_data)
def qCleanupResources():
QtCore.qUnregisterResourceData(rcc_version, qt_resource_struct, qt_resource_name, qt_resource_data)
qInitResources()
入口文件iuMusic.py内容如下:
# -*- coding: utf-8 -*-
import sys
import os
from PyQt5 import QtWidgets, QtMultimedia, QtCore
from PyQt5.QtGui import QIcon
from music_button import Ui_MainWindow
from qrc_files import pause, play
import random
current_dir = os.path.dirname(os.path.abspath(__file__))
music_dir = current_dir + r'\music_datas'
music_files = os.listdir(music_dir)
class MyPlayer(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super(MyPlayer, self).__init__()
self.setupUi(self)
self.setWindowTitle("Umusic")
self.setWindowIcon(QIcon("MusicCloud.ico"))
self.playlist = QtMultimedia.QMediaPlaylist() # 播放列表
self.playlist.setPlaybackMode(QtMultimedia.QMediaPlaylist.Loop) # 列表循环
self.played = QtMultimedia.QMediaPlayer()
self.played.setPlaylist(self.playlist)
self.played.setVolume(50.0)
self.current_index = random.randint(0, len(music_files) - 1)
self.music_path = [(music_dir + '\\' + music_file) for music_file in music_files]
def pushButton\_1\_click(self):
"""上一曲"""
self.current\_index = self.playlist.currentIndex()
self.current\_index -= 1 if self.current\_index != 0 else (len(music\_files) - 1)
self.music\_file = music\_files\[self.current\_index\]
self.music\_path = music\_dir + '\\\\' + self.music\_file
self.playlist.setCurrentIndex(self.current\_index)
# self.played.setMedia(QtMultimedia.QMediaContent(QtCore.QUrl.fromLocalFile(music\_files\[self.current\_index\])))
self.statusBar().showMessage('播放 --> %s' % self.music\_file)
self.played.play()
def pushButton\_2\_click(self):
"""播放/暂停"""
for m in self.music\_path:
self.playlist.addMedia(QtMultimedia.QMediaContent(QtCore.QUrl.fromLocalFile(m)))
# self.playlist.setPlaybackMode(QtMultimedia.QMediaPlaylist.Loop)
self.playlist.setCurrentIndex(self.current\_index)
if self.pushButton\_2.status == 0:
self.pushButton\_2.setStyleSheet("border-image: url(:/qrc\_files/pause.png);")
self.pushButton\_2.status = 1
self.statusBar().showMessage('播放 --> %s' % music\_files\[self.playlist.currentIndex()\]) # TODO 循环时不显示歌名
self.played.play()
elif self.pushButton\_2.status == 1:
self.pushButton\_2.setStyleSheet("border-image: url(:/qrc\_files/play.png);")
self.pushButton\_2.status = 0
self.statusBar().showMessage('暂停') # TODO 暂停再播放重新开始播放这首歌,不是从暂停播放进度那刻接着播放
self.played.pause()
else:
self.statusBar().showMessage('未知')
def pushButton\_3\_click(self):
"""下一曲"""
# self.current\_index += 1 if self.current\_index != (len(music\_files) - 1) else 0
self.current\_index = self.playlist.currentIndex()
self.current\_index += 1 if self.current\_index != (len(music\_files) - 1) else 0
self.music\_file = music\_files\[self.current\_index\]
self.music\_path = music\_dir + '\\\\' + self.music\_file
self.playlist.setCurrentIndex(self.current\_index)
# self.played.setMedia(QtMultimedia.QMediaContent(QtCore.QUrl.fromLocalFile(music\_files\[self.current\_index\])))
self.statusBar().showMessage('播放 --> %s' % self.music\_file)
self.played.play()
def keyPressEvent(self, e): # 替换事件处理器函数
if e.key() == QtCore.Qt.Key\_Q: # 按下q退出程序
self.close()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
player = MyPlayer()
player.move(1000, 600)
player.show()
sys.exit(app.exec_())
music_datas目录放了一些自己喜欢的歌:
PyCharm配置QtDesigner扩展工具,可参考这篇博文~
手机扫一扫
移动阅读更方便
你可能感兴趣的文章