【脚本项目源码】Python制作多功能音乐播放器,打造专属你的音乐播放器
创始人
2024-04-21 22:24:29
0

前言

本文给大家分享的是如何通过利用Python实现多功能音乐播放器,废话不多直接开整~

开发工具

Python版本: 3.6

相关模块:

os模块

sys模块

time模块

random模块

PyQt5模块

环境搭建

安装Python并添加到环境变量,pip安装需要的相关模块即可。

文中完整源码,评论留言获取。

代码实现

初始化

	def __initialize(self):self.setWindowTitle('音乐播放器-Python工程狮')self.setWindowIcon(QIcon('icon.ico'))self.songs_list = []self.song_formats = ['mp3', 'm4a', 'flac', 'wav', 'ogg']self.settingfilename = 'setting.ini'self.player = QMediaPlayer()self.cur_path = os.path.abspath(os.path.dirname(__file__))self.cur_playing_song = ''self.is_switching = Falseself.is_pause = True# 界面元素# --播放时间self.label1 = QLabel('00:00')self.label1.setStyle(QStyleFactory.create('Fusion'))self.label2 = QLabel('00:00')self.label2.setStyle(QStyleFactory.create('Fusion'))# --滑动条self.slider = QSlider(Qt.Horizontal, self)self.slider.sliderMoved[int].connect(lambda: self.player.setPosition(self.slider.value()))self.slider.setStyle(QStyleFactory.create('Fusion'))# --播放按钮self.play_button = QPushButton('播放', self)self.play_button.clicked.connect(self.playMusic)self.play_button.setStyle(QStyleFactory.create('Fusion'))# --上一首按钮self.preview_button = QPushButton('上一首', self)self.preview_button.clicked.connect(self.previewMusic)self.preview_button.setStyle(QStyleFactory.create('Fusion'))# --下一首按钮self.next_button = QPushButton('下一首', self)self.next_button.clicked.connect(self.nextMusic)self.next_button.setStyle(QStyleFactory.create('Fusion'))# --打开文件夹按钮self.open_button = QPushButton('打开文件夹', self)self.open_button.setStyle(QStyleFactory.create('Fusion'))self.open_button.clicked.connect(self.openDir)# --显示音乐列表self.qlist = QListWidget()self.qlist.itemDoubleClicked.connect(self.doubleClicked)self.qlist.setStyle(QStyleFactory.create('windows'))# --如果有初始化setting, 导入settingself.loadSetting()# --播放模式self.cmb = QComboBox()self.cmb.setStyle(QStyleFactory.create('Fusion'))self.cmb.addItem('顺序播放')self.cmb.addItem('单曲循环')self.cmb.addItem('随机播放')# --计时器self.timer = QTimer(self)self.timer.start(1000)self.timer.timeout.connect(self.playByMode)# 界面布局self.grid = QGridLayout()self.setLayout(self.grid)self.grid.addWidget(self.qlist, 0, 0, 5, 10)self.grid.addWidget(self.label1, 0, 11, 1, 1)self.grid.addWidget(self.slider, 0, 12, 1, 1)self.grid.addWidget(self.label2, 0, 13, 1, 1)self.grid.addWidget(self.play_button, 0, 14, 1, 1)self.grid.addWidget(self.next_button, 1, 11, 1, 2)self.grid.addWidget(self.preview_button, 2, 11, 1, 2)self.grid.addWidget(self.cmb, 3, 11, 1, 2)self.grid.addWidget(self.open_button, 4, 11, 1, 2)

根据播放模式播放音乐

	def playByMode(self):if (not self.is_pause) and (not self.is_switching):self.slider.setMinimum(0)self.slider.setMaximum(self.player.duration())self.slider.setValue(self.slider.value() + 1000)self.label1.setText(time.strftime('%M:%S', time.localtime(self.player.position()/1000)))self.label2.setText(time.strftime('%M:%S', time.localtime(self.player.duration()/1000)))# 顺序播放if (self.cmb.currentIndex() == 0) and (not self.is_pause) and (not self.is_switching):if self.qlist.count() == 0:returnif self.player.position() == self.player.duration():self.nextMusic()# 单曲循环elif (self.cmb.currentIndex() == 1) and (not self.is_pause) and (not self.is_switching):if self.qlist.count() == 0:returnif self.player.position() == self.player.duration():self.is_switching = Trueself.setCurPlaying()self.slider.setValue(0)self.playMusic()self.is_switching = False# 随机播放elif (self.cmb.currentIndex() == 2) and (not self.is_pause) and (not self.is_switching):if self.qlist.count() == 0:returnif self.player.position() == self.player.duration():self.is_switching = Trueself.qlist.setCurrentRow(random.randint(0, self.qlist.count()-1))self.setCurPlaying()self.slider.setValue(0)self.playMusic()self.is_switching = False

代码有点多~~

剩余代码

'''打开文件夹'''def openDir(self):self.cur_path = QFileDialog.getExistingDirectory(self, "选取文件夹", self.cur_path)if self.cur_path:self.showMusicList()self.cur_playing_song = ''self.setCurPlaying()self.label1.setText('00:00')self.label2.setText('00:00')self.slider.setSliderPosition(0)self.is_pause = Trueself.play_button.setText('播放')'''导入setting'''def loadSetting(self):if os.path.isfile(self.settingfilename):config = configparser.ConfigParser()config.read(self.settingfilename)self.cur_path = config.get('MusicPlayer', 'PATH')self.showMusicList()'''更新setting'''def updateSetting(self):config = configparser.ConfigParser()config.read(self.settingfilename)if not os.path.isfile(self.settingfilename):config.add_section('MusicPlayer')config.set('MusicPlayer', 'PATH', self.cur_path)config.write(open(self.settingfilename, 'w'))'''显示文件夹中所有音乐'''def showMusicList(self):self.qlist.clear()self.updateSetting()for song in os.listdir(self.cur_path):if song.split('.')[-1] in self.song_formats:self.songs_list.append([song, os.path.join(self.cur_path, song).replace('\\', '/')])self.qlist.addItem(song)self.qlist.setCurrentRow(0)if self.songs_list:self.cur_playing_song = self.songs_list[self.qlist.currentRow()][-1]'''双击播放音乐'''def doubleClicked(self):self.slider.setValue(0)self.is_switching = Trueself.setCurPlaying()self.playMusic()self.is_switching = False'''设置当前播放的音乐'''def setCurPlaying(self):self.cur_playing_song = self.songs_list[self.qlist.currentRow()][-1]self.player.setMedia(QMediaContent(QUrl(self.cur_playing_song)))'''提示'''def Tips(self, message):QMessageBox.about(self, "提示", message)'''播放音乐'''def playMusic(self):if self.qlist.count() == 0:self.Tips('当前路径内无可播放的音乐文件')returnif not self.player.isAudioAvailable():self.setCurPlaying()if self.is_pause or self.is_switching:self.player.play()self.is_pause = Falseself.play_button.setText('暂停')elif (not self.is_pause) and (not self.is_switching):self.player.pause()self.is_pause = Trueself.play_button.setText('播放')'''上一首'''def previewMusic(self):self.slider.setValue(0)if self.qlist.count() == 0:self.Tips('当前路径内无可播放的音乐文件')returnpre_row = self.qlist.currentRow()-1 if self.qlist.currentRow() != 0 else self.qlist.count() - 1self.qlist.setCurrentRow(pre_row)self.is_switching = Trueself.setCurPlaying()self.playMusic()self.is_switching = False'''下一首'''def nextMusic(self):self.slider.setValue(0)if self.qlist.count() == 0:self.Tips('当前路径内无可播放的音乐文件')returnnext_row = self.qlist.currentRow()+1 if self.qlist.currentRow() != self.qlist.count()-1 else 0self.qlist.setCurrentRow(next_row)self.is_switching = Trueself.setCurPlaying()self.playMusic()self.is_switching = False'''run'''
if __name__ == '__main__':app = QApplication(sys.argv)gui = musicPlayer()gui.show()sys.exit(app.exec_())

结果展示

音乐播放器

最后

为了感谢读者们,我想把我最近收藏的一些编程干货分享给大家,回馈每一个读者,希望能帮到你们。

里面有适合小白新手的全套教程给到大家~

快来和小鱼一起成长进步吧!

① 100+多本PythonPDF(主流和经典的书籍应该都有了)

② Python标准库(最全中文版)

③ 爬虫项目源码(四五十个有趣且经典的练手项目及源码)

④ Python基础入门、爬虫、web开发、大数据分析方面的视频(适合小白学习)

⑤ Python学习路线图(告别不入流的学习)

相关内容

热门资讯

【MySQL】锁 锁 文章目录锁全局锁表级锁表锁元数据锁(MDL)意向锁AUTO-INC锁...
【内网安全】 隧道搭建穿透上线... 文章目录内网穿透-Ngrok-入门-上线1、服务端配置:2、客户端连接服务端ÿ...
GCN的几种模型复现笔记 引言 本篇笔记紧接上文,主要是上一篇看写了快2w字,再去接入代码感觉有点...
数据分页展示逻辑 import java.util.Arrays;import java.util.List;impo...
Redis为什么选择单线程?R... 目录专栏导读一、Redis版本迭代二、Redis4.0之前为什么一直采用单线程?三、R...
【已解决】ERROR: Cou... 正确指令: pip install pyyaml
关于测试,我发现了哪些新大陆 关于测试 平常也只是听说过一些关于测试的术语,但并没有使用过测试工具。偶然看到编程老师...
Lock 接口解读 前置知识点Synchronized synchronized 是 Java 中的关键字,...
Win7 专业版安装中文包、汉... 参考资料:http://www.metsky.com/archives/350.htm...
3 ROS1通讯编程提高(1) 3 ROS1通讯编程提高3.1 使用VS Code编译ROS13.1.1 VS Code的安装和配置...
大模型未来趋势 大模型是人工智能领域的重要发展趋势之一,未来有着广阔的应用前景和发展空间。以下是大模型未来的趋势和展...
python实战应用讲解-【n... 目录 如何在Python中计算残余的平方和 方法1:使用其Base公式 方法2:使用statsmod...
学习u-boot 需要了解的m... 一、常用函数 1. origin 函数 origin 函数的返回值就是变量来源。使用格式如下...
常用python爬虫库介绍与简... 通用 urllib -网络库(stdlib)。 requests -网络库。 grab – 网络库&...
药品批准文号查询|药融云-中国... 药品批文是国家食品药品监督管理局(NMPA)对药品的审评和批准的证明文件...
【2023-03-22】SRS... 【2023-03-22】SRS推流搭配FFmpeg实现目标检测 说明: 外侧测试使用SRS播放器测...
有限元三角形单元的等效节点力 文章目录前言一、重新复习一下有限元三角形单元的理论1、三角形单元的形函数(Nÿ...
初级算法-哈希表 主要记录算法和数据结构学习笔记,新的一年更上一层楼! 初级算法-哈希表...
进程间通信【Linux】 1. 进程间通信 1.1 什么是进程间通信 在 Linux 系统中,进程间通信...
【Docker】P3 Dock... Docker数据卷、宿主机与挂载数据卷的概念及作用挂载宿主机配置数据卷挂载操作示例一个容器挂载多个目...