逻辑主程序:
from PyQt5.QtWidgets import QMainWindow, QApplication, QFileDialog, QMessageBox, QDialog, QPushButton, QAbstractItemView
from PyQt5.QtCore import QUrl, Qt
from PyQt5.QtGui import QStandardItemModel, QStandardItem
from Ui_playerUi3 import Ui_MainWindow
from Mp3Info import GetMp3Info
from PyQt5.QtMultimedia import QMediaPlayer, QMediaPlaylist, QMediaContent
import sys
import os
class My(QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
win = self.setupUi(self)
self.player = QMediaPlayer() # 创建播放器
self.playList = QMediaPlaylist() # 创建播放列表
self.playList.setPlaybackMode(QMediaPlaylist.Loop) # 设置循环播放
self.determineExistPlayDir()
self.singalAndSlot()
self.setWindowFlags(Qt.WindowCloseButtonHint) # 取消窗口最大化和最小化
self.songListSet()
def songListSet(self):
'''在界面显示歌曲列表'''
self.model = QStandardItemModel(0, 4) # 创造数据模型,0行,4列
self.model.setHorizontalHeaderLabels(['歌曲', '歌手', '专辑', '时长']) # 设置标题
if self.playList.mediaCount(): # 添加数据
for index in range(self.playList.mediaCount()):
path = self.playList.media(index).canonicalUrl().path()[1:]
info = GetMp3Info(path)
self.model.appendRow([ # 添加一行数据
QStandardItem(info.title),
QStandardItem(info.artist),
QStandardItem(info.album),
QStandardItem(info.length)
])
self.songList.setModel(self.model) # 把数据添加至QtableView中
self.songList.setColumnWidth(0, 350) # 设置第1列列宽
self.songList.setColumnWidth(1, 200) # 设置第2列列宽
self.songList.setColumnWidth(2, 250) # 设置第3列列宽
self.songList.setColumnWidth(3, 158) # 设置第4列列宽
self.songList.setEditTriggers(
QAbstractItemView.NoEditTriggers) # 设置不可编辑单元格
def singalAndSlot(self):
'''这里汇聚了所有的信号和槽'''
self.nextSong.clicked.connect(self.nextS)
self.preSong.clicked.connect(self.preS)
self.startStop.clicked.connect(self.startOrStop)
self.songDir.clicked.connect(self.writeDir)
self.songList.doubleClicked.connect(self.otherSong)
def otherSong(self):
'''双击后播放'''
index = self.songList.currentIndex().row() # 获取双击所在行
self.playList.setCurrentIndex(index)
self.sta