PyQt5实现简易音乐播放器
- 环境
- vscode
- python 3.10.0
- PyQt5 5.15.4
- 功能目标
能够读取本地的音乐文件,并实现播放的开关、曲目的切换和音量的加减
- 具体实现
- 新建一个文件夹,在文件夹下再新建一个文件夹,命名为music,将歌曲放入其中。在vscode中打开该文件夹。
- 在QtDesigner中搭建出UI界面,并使用pyuic工具转换为py文件
- 新建main.py文件,用于界面的显示
- 新建一个文件夹,在文件夹下再新建一个文件夹,命名为music,将歌曲放入其中。在vscode中打开该文件夹。
import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow
from Ui_musicplayer import Ui_Dialog
class MainWin(QMainWindow, Ui_Dialog):
def __init__(self, parent=None):
super(MainWin, self).__init__(parent)
self.setupUi(self)
if __name__ == '__main__':
# 屏幕适配处理
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
main = MainWin()
main.show()
sys.exit(app.exec_())
4. 通过os.listdir()函数完成歌曲文件的读取
... # 省略之前出现的内容,后面的...同理
import os
class MainWin(QMainWindow, Ui_Dialog):
def __init__(self, parent=None):
super(MainWin, self).__init__(parent)
self.setupUi(self)
# 歌曲存放目录
self.mp3filedir = "./music/"
# 用于存放歌曲名称的空列表
self.music_list = []
# 当前播放歌曲在列表中的位置,开始为0
self.music_index = 0
# 从歌曲存放目录中读取所有文件名,并放入列表中
for file_name in os.listdir(self.mp3filedir):
self.music_list.append(file_name)
# 获取歌曲总数
self.music_all_num = len(self.music_list)
5. 新建 QMediaPlayer对象,并完成音量的设置
...
from PyQt5.QtCore import QUrl
from PyQt5.QtMultimedia import QMediaPlayer, QMediaContent
class MainWin(QMainWindow, Ui_Dialog):
def __init__(self, parent=None):
super(MainWin, self).__init__(parent)
self.setupUi(self)
...
# 新建QMediaPlayer对象
self.player = QMediaPlayer()
# 当前播放状态标志位,False表示当前处于暂停状态,True表示当前处于播放状态
self.player_state = False
# 音量设定
self.voice_level = 50
# 音量设置,0-100,设置为0将静音
self.player.setVolume(self.voice_level)
# 拼接歌曲存放目录和歌曲名成完整路径,获取到文件
self.player.setMedia(QMediaContent(QUrl.fromLocalFile(self.mp3filedir + self.music_list[self.music_index])))
6. 歌曲名字的展示
...
class MainWin(QMainWindow, Ui_Dialog):
def __init__(self, parent=None):
super(MainWin, self).__init__(parent)
self.setupUi(self)
...
# 歌曲名展示
self.music_detail()
def music_detail(self):
# 根据当前歌曲位置索引获取歌曲名
self.label.setText(self.music_list[self.music_index])
效果图文章来源:https://www.toymoban.com/news/detail-537096.html
文章来源地址https://www.toymoban.com/news/detail-537096.html
7. 播放按钮绑定函数,t实现歌曲播放状态的切换
...
class MainWin(QMainWindow, Ui_Dialog):
def __init__(self, parent=None):
super(MainWin, self).__init__(parent)
self.setupUi(self)
...
# 播放按钮绑定函数
self.pushButton.clicked.connect(self.player_state_change)
def player_state_change(self):
# 通过self.player_state获取当前播放状态,再进行切换
if self.player_state:
# 停止播放
self.player.stop()
# 播放状态标志设置为False
self.player_state = False
else:
# 播放当前音频
self.player.play()
# 播放状态标志设置为True
self.player_state = True
8. 歌曲上下切换按钮绑定函数,实现歌曲的切换
...
class MainWin(QMainWindow, Ui_Dialog):
def __init__(self, parent=None):
super(MainWin, self).__init__(parent)
self.setupUi(self)
...
# 下一首按钮点击事件绑定函数
self.pushButton_2.clicked.connect(self.next_music)
# 上一首按钮点击事件绑定函数
self.pushButton_3.clicked.connect(self.previous_music)
def next_music(self):
# 如果当前播放的音频位置索引为列表中最后一首,则位置索引置为0,回到列表开头
if self.music_index == self.music_all_num-1:
self.music_index = 0
# 否则位置索引加1
else:
self.music_index += 1
# 根据位置索引获取歌曲名,并和歌曲存放目录拼接获取文件
self.player.setMedia(QMediaContent(QUrl.fromLocalFile(self.mp3filedir + self.music_list[self.music_index])))
# 歌曲名展示更新
self.music_detail()
# 进行播放
self.player.play()
def previous_music(self):
# 如果当前播放的音频位置索引为列表中第一首,则位置索引置为列表长度减一,到列表末尾
if self.music_index == 0:
self.music_index = self.music_all_num - 1
# 否则位置索引减1
else:
self.music_index -= 1
# 根据位置索引获取歌曲名,并和歌曲存放目录拼接获取文件
self.player.setMedia(QMediaContent(QUrl.fromLocalFile(self.mp3filedir + self.music_list[self.music_index])))
# 歌曲名展示更新
self.music_detail()
# 进行播放
self.player.play()
9. 音量的增减,直接修改音量设定值再执行次音量设定函数即可
...
class MainWin(QMainWindow, Ui_Dialog):
def __init__(self, parent=None):
super(MainWin, self).__init__(parent)
self.setupUi(self)
...
self.pushButton_4.clicked.connect(self.voice_add)
self.pushButton_5.clicked.connect(self.voice_reduce)
def voice_add(self):
if self.voice_level < 100:
self.voice_level += 10
self.player.setVolume(self.voice_level)
def voice_reduce(self):
if self.voice_level > 0:
self.voice_level -= 10
self.player.setVolume(self.voice_level)
- 主函数代码汇总
import sys
import os
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtCore import QUrl
from PyQt5.QtMultimedia import QMediaPlayer, QMediaContent
from Ui_musicplayer import Ui_Dialog
class MainWin(QMainWindow, Ui_Dialog):
def __init__(self, parent=None):
super(MainWin, self).__init__(parent)
self.setupUi(self)
# 新建QMediaPlayer对象
self.player = QMediaPlayer()
# 当前播放状态标志位,False表示当前处于暂停状态,True表示当前处于播放状态
self.player_state = False
# 歌曲存放目录
self.mp3filedir = "./music/"
# 用于存放歌曲名称的空列表
self.music_list = []
# 当前播放歌曲在列表中的位置
self.music_index = 0
# 音量设置
self.voice_level = 50
# 从歌曲存放目录中读取所有文件名,并放入列表中
for file_name in os.listdir(self.mp3filedir):
self.music_list.append(file_name)
# 获取歌曲总数
self.music_all_num = len(self.music_list)
# 拼接歌曲存放目录和歌曲名成完整路径,获取到文件
self.player.setMedia(QMediaContent(QUrl.fromLocalFile(self.mp3filedir + self.music_list[self.music_index])))
# 文件名展示
self.music_detail()
# 音量设置
self.player.setVolume(self.voice_level)
# 播放按钮绑定函数
self.pushButton.clicked.connect(self.player_state_change)
# 下一首按钮绑定函数
self.pushButton_2.clicked.connect(self.next_music)
# 上一首按钮绑定函数
self.pushButton_3.clicked.connect(self.previous_music)
# 音量加按钮绑定函数
self.pushButton_4.clicked.connect(self.voice_add)
# 音量减按钮绑定函数
self.pushButton_5.clicked.connect(self.voice_reduce)
def player_state_change(self):
# 通过self.player_state获取当前播放状态,再进行切换
if self.player_state:
# 停止播放
self.player.stop()
# 播放状态标志设置为False
self.player_state = False
else:
# 播放当前音频
self.player.play()
# 播放状态标志设置为True
self.player_state = True
def next_music(self):
# 如果当前播放的音频位置索引为列表中最后一首,则位置索引置为0,回到列表开头
if self.music_index == self.music_all_num-1:
self.music_index = 0
# 否则位置索引加1
else:
self.music_index += 1
# 根据位置索引获取歌曲名,并和歌曲存放目录拼接获取文件
self.player.setMedia(QMediaContent(QUrl.fromLocalFile(self.mp3filedir + self.music_list[self.music_index])))
# 歌曲名展示更新
self.music_detail()
# 进行播放
self.player.play()
def previous_music(self):
# 如果当前播放的音频位置索引为列表中第一首,则位置索引置为列表长度减一,到列表末尾
if self.music_index == 0:
self.music_index = self.music_all_num - 1
# 否则位置索引减1
else:
self.music_index -= 1
# 根据位置索引获取歌曲名,并和歌曲存放目录拼接获取文件
self.player.setMedia(QMediaContent(QUrl.fromLocalFile(self.mp3filedir + self.music_list[self.music_index])))
# 歌曲名展示更新
self.music_detail()
# 进行播放
self.player.play()
def music_detail(self):
self.label.setText(self.music_list[self.music_index])
def voice_add(self):
if self.voice_level < 100:
self.voice_level += 10
self.player.setVolume(self.voice_level)
def voice_reduce(self):
if self.voice_level > 0:
self.voice_level -= 10
self.player.setVolume(self.voice_level)
if __name__ == '__main__':
# 屏幕分辨率适配
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
main = MainWin()
main.show()
sys.exit(app.exec_())
- 布局文件
# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(400, 300)
self.groupBox = QtWidgets.QGroupBox(Dialog)
self.groupBox.setGeometry(QtCore.QRect(0, 10, 401, 251))
self.groupBox.setObjectName("groupBox")
self.label = QtWidgets.QLabel(self.groupBox)
self.label.setGeometry(QtCore.QRect(10, 20, 381, 221))
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.layoutWidget = QtWidgets.QWidget(Dialog)
self.layoutWidget.setGeometry(QtCore.QRect(0, 270, 401, 25))
self.layoutWidget.setObjectName("layoutWidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.layoutWidget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.pushButton_4 = QtWidgets.QPushButton(self.layoutWidget)
self.pushButton_4.setObjectName("pushButton_4")
self.horizontalLayout.addWidget(self.pushButton_4)
self.pushButton_2 = QtWidgets.QPushButton(self.layoutWidget)
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout.addWidget(self.pushButton_2)
self.pushButton = QtWidgets.QPushButton(self.layoutWidget)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout.addWidget(self.pushButton)
self.pushButton_3 = QtWidgets.QPushButton(self.layoutWidget)
self.pushButton_3.setObjectName("pushButton_3")
self.horizontalLayout.addWidget(self.pushButton_3)
self.pushButton_5 = QtWidgets.QPushButton(self.layoutWidget)
self.pushButton_5.setObjectName("pushButton_5")
self.horizontalLayout.addWidget(self.pushButton_5)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.groupBox.setTitle(_translate("Dialog", "详情"))
self.label.setText(_translate("Dialog", "TextLabel"))
self.pushButton_4.setText(_translate("Dialog", "音量+"))
self.pushButton_2.setText(_translate("Dialog", "上一首"))
self.pushButton.setText(_translate("Dialog", "播放"))
self.pushButton_3.setText(_translate("Dialog", "下一首"))
self.pushButton_5.setText(_translate("Dialog", "音量-"))
到了这里,关于PyQt5实现简易音乐播放器的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!