Python 使用PyQt5.QtMultimedia和mutagen写的一个小的MP3播放器

UI超级简单

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(218, 68)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.widget = QtWidgets.QWidget(self.centralwidget)
        self.widget.setGeometry(QtCore.QRect(10, 10, 196, 32))
        self.widget.setObjectName("widget")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.widget)
        self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.preSong = QtWidgets.QPushButton(self.widget)
        self.preSong.setMinimumSize(QtCore.QSize(60, 30))
        self.preSong.setMaximumSize(QtCore.QSize(60, 30))
        self.preSong.setToolTipDuration(0)
        self.preSong.setStyleSheet("background:white; border:0px;border-radius:10px")
        self.preSong.setObjectName("preSong")
        self.horizontalLayout.addWidget(self.preSong)
        self.startStop = QtWidgets.QPushButton(self.widget)
        self.startStop.setMinimumSize(QtCore.QSize(60, 30))
        self.startStop.setMaximumSize(QtCore.QSize(60, 30))
        self.startStop.setToolTipDuration(0)
        self.startStop.setStyleSheet("background:white; border:0px;border-radius:10px")
        self.startStop.setObjectName("startStop")
        self.horizontalLayout.addWidget(self.startStop)
        self.nextSong = QtWidgets.QPushButton(self.widget)
        self.nextSong.setMinimumSize(QtCore.QSize(60, 30))
        self.nextSong.setMaximumSize(QtCore.QSize(60, 30))
        self.nextSong.setToolTipDuration(0)
        self.nextSong.setStyleSheet("background:white; border:0px;border-radius:10px")
        self.nextSong.setObjectName("nextSong")
        self.horizontalLayout.addWidget(self.nextSong)
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Mp3Player"))
        self.preSong.setText(_translate("MainWindow", "上一首"))
        self.startStop.setText(_translate("MainWindow", "暂停"))
        self.nextSong.setText(_translate("MainWindow", "下一首"))



逻辑:

from PyQt5.QtWidgets import QMainWindow,QApplication
from PyQt5.QtCore import QUrl
from Ui_playerUi import Ui_MainWindow
from Mp3Info import GetMp3Info
from PyQt5.QtMultimedia import QMediaPlayer,QMediaPlaylist,QMediaContent
import sys,os
class My(QMainWindow,Ui_MainWindow):
    def __init__(self):
        super().__init__()
        win=self.setupUi(self)
        self.getSongs()
        self.player=QMediaPlayer()  #创建播放器
        self.playList=QMediaPlaylist() #创建播放列表
        self.playList.setPlaybackMode(QMediaPlaylist.Loop)  #设置循环播放
        for i in self.allSong:
            self.playList.addMedia(QMediaContent(QUrl.fromLocalFile(i))) #把所有歌曲添加到列表中
        self.player.setPlaylist(self.playList)
        self.player.play()#开始播放
        songInfo=self.getSongInfo()
        self.statusbar.showMessage(songInfo.title)
        self.nextSong.clicked.connect(self.nextS)
        self.preSong.clicked.connect(self.preS)
        self.startStop.clicked.connect(self.startOrStop)

    def startOrStop(self):
        '''播放按键控制'''
        if self.player.state()==1:
            self.player.pause()
            self.statusbar.showMessage('暂停')
            self.startStop.setText('播放')
        elif self.player.state()==2:
            self.player.play()
            self.staShow()
            self.startStop.setText('暂停')

    def staShow(self):
        '''显示歌曲名'''
        songInfo=self.getSongInfo()
        self.statusbar.showMessage(songInfo.title)

    def nextS(self):
        '''下一首'''
        self.playList.next()
        self.staShow()

    def preS(self):
        '''上一首'''
        self.playList.previous()
        self.staShow()

    def getSongInfo(self):
        '''获取歌曲信息
        info:歌曲信息对象
        '''
        path=self.player.currentMedia().canonicalUrl().path()[1:] #获取当前播放曲目路径
        info=GetMp3Info(path)
        return info
        


    def getSongs(self):
        '''
        获取文件夹内所有的歌曲
        '''
        url='g:/songs1'
        self.allSong=[]
        for root,dirs,files in os.walk(url):
            for file in files:
                self.allSong.append(os.path.join(root,file))



if __name__=="__main__":
    app=QApplication(sys.argv)
    w=My()
    w.show()
    sys.exit(app.exec())

from Mp3Info import GetMp3Info这条语句的内容请看我上一篇文章吧,我就不重复粘贴了^ ^:https://blog.csdn.net/weixin_38587484/article/details/97792461

你可能感兴趣的:(Python)