IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Python知识库 -> Python实用工具,pyqt5模块,Python实现简易版音乐播放器 -> 正文阅读

[Python知识库]Python实用工具,pyqt5模块,Python实现简易版音乐播放器

前言:

利用Python制作一款简易音乐播放器,让我们愉快地开始吧~

图片仅靠参考

开发工具

**Python版本:**3.6.4

相关模块:

pyqt5模块;

以及一些Python自带的模块。

环境搭建

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

原理简介

其实相关文件中的源代码我已经做了一些注释,会pyqt5的话基本看下源码就懂了,因为原理还是很简单的。这里就简单介绍一下吧。

一.设计界面

QAQ界面设计的比较简约,大概长这个样子:

图片

源代码里一个个地定义界面包含的元素,然后排版一下就行了:

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, 导入setting
		self.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)

二. 实现各部分功能

(1)存放音乐的文件夹选取

直接调pyqt5相应的函数就行:

'''打开文件夹'''
	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 = True
			self.play_button.setText('播放')

打开文件夹后把所有的音乐文件显示在界面左侧,并保存一些必要的信息:

'''显示文件夹中所有音乐'''
	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]

(2)音乐播放

音乐播放功能直接调用QMediaPlayer实现:

'''播放音乐'''
	def playMusic(self):
		if self.qlist.count() == 0:
			self.Tips('当前路径内无可播放的音乐文件')
			return
		if not self.player.isAudioAvailable():
			self.setCurPlaying()
		if self.is_pause or self.is_switching:
			self.player.play()
			self.is_pause = False
			self.play_button.setText('暂停')
		elif (not self.is_pause) and (not self.is_switching):
			self.player.pause()
			self.is_pause = True
			self.play_button.setText('播放')

(3)音乐切换

点击上一首/下一首按钮切换:

'''上一首'''
	def previewMusic(self):
		self.slider.setValue(0)
		if self.qlist.count() == 0:
			self.Tips('当前路径内无可播放的音乐文件')
			return
		pre_row = self.qlist.currentRow()-1 if self.qlist.currentRow() != 0 else self.qlist.count() - 1
		self.qlist.setCurrentRow(pre_row)
		self.is_switching = True
		self.setCurPlaying()
		self.playMusic()
		self.is_switching = False
	'''下一首'''
	def nextMusic(self):
		self.slider.setValue(0)
		if self.qlist.count() == 0:
			self.Tips('当前路径内无可播放的音乐文件')
			return
		next_row = self.qlist.currentRow()+1 if self.qlist.currentRow() != self.qlist.count()-1 else 0
		self.qlist.setCurrentRow(next_row)
		self.is_switching = True
		self.setCurPlaying()
		self.playMusic()
		self.is_switching = False

双击某首歌切换:

'''双击播放音乐'''
	def doubleClicked(self):
		self.slider.setValue(0)
		self.is_switching = True
		self.setCurPlaying()
		self.playMusic()
		self.is_switching = False

根据播放模式切换:

'''根据播放模式播放音乐'''
	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:
				return
			if 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:
				return
			if self.player.position() == self.player.duration():
				self.is_switching = True
				self.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:
				return
			if self.player.position() == self.player.duration():
				self.is_switching = True
				self.qlist.setCurrentRow(random.randint(0, self.qlist.count()-1))
				self.setCurPlaying()
				self.slider.setValue(0)
				self.playMusic()
				self.is_switching = False

文章到这里就结束了,感谢你的观看,关注我每天分享Python小工具系列,下篇文章分享利用邮件远程控制自己电脑

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

点击干货主要有获取

① 2000多本Python电子书(主流和经典的书籍应该都有了)

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

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

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

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

⑥ 两天的Python爬虫训练营直播权限

All done~完整源代码+干货加Python新手学习交流群:594356095

  Python知识库 最新文章
Python中String模块
【Python】 14-CVS文件操作
python的panda库读写文件
使用Nordic的nrf52840实现蓝牙DFU过程
【Python学习记录】numpy数组用法整理
Python学习笔记
python字符串和列表
python如何从txt文件中解析出有效的数据
Python编程从入门到实践自学/3.1-3.2
python变量
上一篇文章      下一篇文章      查看所有文章
加:2021-07-10 14:30:21  更:2021-07-10 14:30:23 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/3 13:20:41-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码