用pyqt5做一个简易的音乐播放器
程序员文章站
2022-04-26 16:04:27
...
前言
没什么,就是无聊
需求
要求可以读取音频文档,有播放和暂停的功能
附上代码
(1)UI界面
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'UiForm.ui'
#
# Created by: PyQt5 UI code generator 5.15.1
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(750, 350)
self.btn_readMusic = QtWidgets.QPushButton(Form)
self.btn_readMusic.setGeometry(QtCore.QRect(140, 200, 93, 28))
self.btn_readMusic.setObjectName("btn_readMusic")
self.btn_play = QtWidgets.QPushButton(Form)
self.btn_play.setGeometry(QtCore.QRect(310, 200, 93, 28))
self.btn_play.setObjectName("btn_play")
self.btn_pause = QtWidgets.QPushButton(Form)
self.btn_pause.setGeometry(QtCore.QRect(490, 200, 93, 28))
self.btn_pause.setObjectName("btn_pause")
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(50, 110, 81, 21))
self.label.setObjectName("label")
self.label_MusicMessage = QtWidgets.QLabel(Form)
self.label_MusicMessage.setGeometry(QtCore.QRect(140, 110, 591, 21))
self.label_MusicMessage.setObjectName("label_MusicMessage")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.btn_readMusic.setText(_translate("Form", "读取音频"))
self.btn_play.setText(_translate("Form", "播放"))
self.btn_pause.setText(_translate("Form", "停止"))
self.label.setText(_translate("Form", "当前播放:"))
self.label_MusicMessage.setText(_translate("Form", "暂无"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
(2)逻辑处理类
# -*- coding: utf-8 -*-
# @Time : 2021/4/2 22:23
# @Author : Zeeland
# @File : FunEdit.py
# @Software: PyCharm
from PyQt5.QtWidgets import QWidget,QApplication,QFileDialog,QMessageBox
from UiForm import Ui_Form
import sys,time,pygame
class FunEdit(QWidget,Ui_Form):
'''
初始化各方面信息
'''
def __init__(self):
super(FunEdit, self).__init__()
self.setupUi(self)#Ui初始化
self.init()
'''
该方法用于信号与槽的绑定
'''
def init(self):
self.btn_readMusic.clicked.connect(self.readMusic)
self.btn_play.clicked.connect(self.start)
self.btn_pause.clicked.connect(self.pause)
'''
读取音频
'''
def readMusic(self):
#读取特定格式的文件,传入一个self,"标题名","初始显示文件夹的路径","特定格式(以两个分号区分)"
#该方法返回一个Tuple,分别为str类型的路径名和file的type(eg:MP3)
self.fdir,self.ftype =QFileDialog.getOpenFileName(self,"Open File","","Mp3(*.mp3);;Wav(*.wav)")
# 判断是否为空路径,如果为空路径,则不能加载音频,否则会闪退
if self.fdir=="":
print("没有选择文件")
return
print(self.fdir)
pygame.init()#初始化音乐播放装置,初始化后才可以使用
self.track =pygame.mixer.music.load(str(self.fdir))#加载音频文件(放入缓存池)
self.label_MusicMessage.setText(str(self.fdir))
'''
播放
'''
def start(self):
if self.label_MusicMessage.text()=='暂无':
QMessageBox.about(self,'message','当前暂无播放的音乐')
return
pygame.mixer.music.play()#播放
'''
停止播放
'''
def pause(self):
if self.label_MusicMessage.text()=='暂无':
QMessageBox.about(self,'message','当前暂无播放的音乐')
return
pygame.mixer.music.stop()#暂停
self.label_MusicMessage.setText("暂停中")
'''
主方法运行入口
'''
if __name__ == '__main__':
app = QApplication(sys.argv)
win = FunEdit()
win.show()
sys.exit(app.exec())