关键词搜索

源码搜索 ×
×

Python实用工具,pyqt5模块,Python实现简易版音乐播放器

发布2021-08-09浏览798次

详情内容

开发工具

Python版本:3.6.4

相关模块:

pyqt5模块;

以及一些Python自带的模块。

环境搭建

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

原理简介

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

一.设计界面

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

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

  1. self.label1 = QLabel('00:00')
  2. self.label1.setStyle(QStyleFactory.create('Fusion'))
  3. self.label2 = QLabel('00:00')
  4. self.label2.setStyle(QStyleFactory.create('Fusion'))
  5. # --滑动条
  6. self.slider = QSlider(Qt.Horizontal, self)
  7. self.slider.sliderMoved[int].connect(lambda: self.player.setPosition(self.slider.value()))
  8. self.slider.setStyle(QStyleFactory.create('Fusion'))
  9. # --播放按钮
  10. self.play_button = QPushButton('播放', self)
  11. self.play_button.clicked.connect(self.playMusic)
  12. self.play_button.setStyle(QStyleFactory.create('Fusion'))
  13. # --上一首按钮
  14. self.preview_button = QPushButton('上一首', self)
  15. self.preview_button.clicked.connect(self.previewMusic)
  16. self.preview_button.setStyle(QStyleFactory.create('Fusion'))
  17. # --下一首按钮
  18. self.next_button = QPushButton('下一首', self)
  19. self.next_button.clicked.connect(self.nextMusic)
  20. self.next_button.setStyle(QStyleFactory.create('Fusion'))
  21. # --打开文件夹按钮
  22. self.open_button = QPushButton('打开文件夹', self)
  23. self.open_button.setStyle(QStyleFactory.create('Fusion'))
  24. self.open_button.clicked.connect(self.openDir)
  25. # --显示音乐列表
  26. self.qlist = QListWidget()
  27. self.qlist.itemDoubleClicked.connect(self.doubleClicked)
  28. self.qlist.setStyle(QStyleFactory.create('windows'))
  29. # --如果有初始化setting, 导入setting
  30. self.loadSetting()
  31. # --播放模式
  32. self.cmb = QComboBox()
  33. self.cmb.setStyle(QStyleFactory.create('Fusion'))
  34. self.cmb.addItem('顺序播放')
  35. self.cmb.addItem('单曲循环')
  36. self.cmb.addItem('随机播放')
  37. # --计时器
  38. self.timer = QTimer(self)
  39. self.timer.start(1000)
  40. self.timer.timeout.connect(self.playByMode)
  41. # 界面布局
  42. self.grid = QGridLayout()
  43. self.setLayout(self.grid)
  44. self.grid.addWidget(self.qlist, 0, 0, 5, 10)
  45. self.grid.addWidget(self.label1, 0, 11, 1, 1)
  46. self.grid.addWidget(self.slider, 0, 12, 1, 1)
  47. self.grid.addWidget(self.label2, 0, 13, 1, 1)
  48. self.grid.addWidget(self.play_button, 0, 14, 1, 1)
  49. self.grid.addWidget(self.next_button, 1, 11, 1, 2)
  50. self.grid.addWidget(self.preview_button, 2, 11, 1, 2)
  51. self.grid.addWidget(self.cmb, 3, 11, 1, 2)
  52. self.grid.addWidget(self.open_button, 4, 11, 1, 2)

二. 实现各部分功能

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

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

  1. '''打开文件夹'''
  2. def openDir(self):
  3. self.cur_path = QFileDialog.getExistingDirectory(self, "选取文件夹", self.cur_path)
  4. if self.cur_path:
  5. self.showMusicList()
  6. self.cur_playing_song = ''
  7. self.setCurPlaying()
  8. self.label1.setText('00:00')
  9. self.label2.setText('00:00')
  10. self.slider.setSliderPosition(0)
  11. self.is_pause = True
  12. self.play_button.setText('播放')

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

  1. '''显示文件夹中所有音乐'''
  2. def showMusicList(self):
  3. self.qlist.clear()
  4. self.updateSetting()
  5. for song in os.listdir(self.cur_path):
  6. if song.split('.')[-1] in self.song_formats:
  7. self.songs_list.append([song, os.path.join(self.cur_path, song).replace('\\', '/')])
  8. self.qlist.addItem(song)
  9. self.qlist.setCurrentRow(0)
  10. if self.songs_list:
  11. self.cur_playing_song = self.songs_list[self.qlist.currentRow()][-1]

(2)音乐播放

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

  1. '''播放音乐'''
  2. def playMusic(self):
  3. if self.qlist.count() == 0:
  4. self.Tips('当前路径内无可播放的音乐文件')
  5. return
  6. if not self.player.isAudioAvailable():
  7. self.setCurPlaying()
  8. if self.is_pause or self.is_switching:
  9. self.player.play()
  10. self.is_pause = False
  11. self.play_button.setText('暂停')
  12. elif (not self.is_pause) and (not self.is_switching):
  13. self.player.pause()
  14. self.is_pause = True
  15. self.play_button.setText('播放')

(3)音乐切换

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

  1. '''上一首'''
  2. def previewMusic(self):
  3. self.slider.setValue(0)
  4. if self.qlist.count() == 0:
  5. self.Tips('当前路径内无可播放的音乐文件')
  6. return
  7. pre_row = self.qlist.currentRow()-1 if self.qlist.currentRow() != 0 else self.qlist.count() - 1
  8. self.qlist.setCurrentRow(pre_row)
  9. self.is_switching = True
  10. self.setCurPlaying()
  11. self.playMusic()
  12. self.is_switching = False
  13. '''下一首'''
  14. def nextMusic(self):
  15. self.slider.setValue(0)
  16. if self.qlist.count() == 0:
  17. self.Tips('当前路径内无可播放的音乐文件')
  18. return
  19. next_row = self.qlist.currentRow()+1 if self.qlist.currentRow() != self.qlist.count()-1 else 0
  20. self.qlist.setCurrentRow(next_row)
  21. self.is_switching = True
  22. self.setCurPlaying()
  23. self.playMusic()
  24. self.is_switching = False

双击某首歌切换:

  1. '''双击播放音乐'''
  2. def doubleClicked(self):
  3. self.slider.setValue(0)
  4. self.is_switching = True
  5. self.setCurPlaying()
  6. self.playMusic()
  7. self.is_switching = False

根据播放模式切换:

  1. '''根据播放模式播放音乐'''
  2. def playByMode(self):
  3. if (not self.is_pause) and (not self.is_switching):
  4. self.slider.setMinimum(0)
  5. self.slider.setMaximum(self.player.duration())
  6. self.slider.setValue(self.slider.value() + 1000)
  7. self.label1.setText(time.strftime('%M:%S', time.localtime(self.player.position()/1000)))
  8. self.label2.setText(time.strftime('%M:%S', time.localtime(self.player.duration()/1000)))
  9. # 顺序播放
  10. if (self.cmb.currentIndex() == 0) and (not self.is_pause) and (not self.is_switching):
  11. if self.qlist.count() == 0:
  12. return
  13. if self.player.position() == self.player.duration():
  14. self.nextMusic()
  15. # 单曲循环
  16. elif (self.cmb.currentIndex() == 1) and (not self.is_pause) and (not self.is_switching):
  17. if self.qlist.count() == 0:
  18. return
  19. if self.player.position() == self.player.duration():
  20. self.is_switching = True
  21. self.setCurPlaying()
  22. self.slider.setValue(0)
  23. self.playMusic()
  24. self.is_switching = False
  25. # 随机播放
  26. elif (self.cmb.currentIndex() == 2) and (not self.is_pause) and (not self.is_switching):
  27. if self.qlist.count() == 0:
  28. return
  29. if self.player.position() == self.player.duration():
  30. self.is_switching = True
  31. self.qlist.setCurrentRow(random.randint(0, self.qlist.count()-1))
  32. self.setCurPlaying()
  33. self.slider.setValue(0)
  34. self.playMusic()
  35. self.is_switching = False

文章到vb.net教程这里就c#教程结束了,感谢python教程你的观看,下篇文章分享利用邮件远程控制自己电脑

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

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载