首页 > 编程语言 >python学习记录:简易音乐播放器源码

python学习记录:简易音乐播放器源码

时间:2022-10-17 15:45:25浏览次数:53  
标签:播放器 player python self switching slider 源码 qlist button

'''
Function:
  
音乐播放器
Author
  
琴棋书画
'''
import os
import sys
import time
import random
import configparser
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtMultimedia import *


'''音乐播放器'''
class musicPlayer(QWidget):
   def __init__(self):
      super().__init__()
      self.__initialize()
   '''初始化'''
   def __initialize(self):
      self.setWindowTitle('音乐播放器v1.0 琴棋书画')
      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 = False
      self.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, 导入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)
   '''根据播放模式播放音乐'''
   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
   '''打开文件夹'''
   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('播放')
   '''导入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 = True
      self.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('当前路径内无可播放的音乐文件')
         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('播放')
   '''上一首'''
   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


'''run'''
if __name__ == '__main__':
   app = QApplication(sys.argv)
   gui = musicPlayer()
   gui.show()
   sys.exit(app.exec_())

标签:播放器,player,python,self,switching,slider,源码,qlist,button
From: https://www.cnblogs.com/qlsh/p/16799406.html

相关文章

  • 源码分析之序列化器的many关键字
    在序列多个数据时,我们需要指定一个关键字many=True这是为什么呢?其实是,当序列化器产生对象时,传入参数many和不传入会生成两个不同的对象!!这是怎么实现的呢??1.类的对象生......
  • python中的range()函数
    range()函数:用于生成一个整数序列; range()的三种创建方式: 第一种:只有一个参数(小括号中只给了一个数)即range(stop)  例如:range(10)指的是默认从0开始,步长为1,不包括10......
  • 【GIS开发】Esri Shapefile(.shp)矢量数据文件读取(C++、Python)
    1、简介1.1什么是Shapefile<fontcolor=blue>ESRIShapefile(shp),或简称shapefile,是美国环境系统研究所公司(ESRI)开发的一种空间数据开放格式。该文件格式已经成为了地理信......
  • 「http代理」Python-Scrapy 代码样例(一)
    http代理使用提示1.http/https网页均可适用2.scrapy不是python原生库,需要安装才能使用:pipinstallscrapy3.在第一级tutorial目录下运行如下命令查看......
  • python学习记录:学生点名系统源码
     学生点名系统#coding=utf-8importtkinterastkfromtkinterimport*importrandomimporttimeimportopenpyxl is_run=False  defget_students_n......
  • python学习记录;笔趣阁小说全文下载工具爬虫源码
    笔趣阁小说全本下载工具#导入模块importrequestsimportparselfromlxmlimportetreeimportreimportpandasaspdimportdatetimeimporttimefromtqdmimpor......
  • Python3爬虫中Selenium的用法详解
    Selenium是一个自动化测试工具,利用它可以驱动浏览器执行特定的动作,如点击、下拉等操作,同时还可以获取浏览器当前呈现的页面的源代码,做到可见即可爬。对于一些JavaScript动......
  • js 简单封装一个像python中的range函数
    functionrange(start,end,step){letarr=[];for(leti=start;i<end;i++){if(i%step==0){arr.push(i)}}returnarr;}letarr=range(0,60,5);conso......
  • ConcurrentHashMap源码,看我这篇就够了
    思考:HashTable是线程安全的,为什么不推荐使用?HashTable是一个线程安全的类,它使用synchronized来锁住整张Hash表来实现线程安全,即每次锁住整张表让线程独占,相当于所有线程进......
  • ConcurrentHashMap源码,看我这篇就够了
    持续创作,加速成长!这是我参与「掘金日新计划·10月更文挑战」的第5天,点击查看活动详情思考:HashTable是线程安全的,为什么不推荐使用?HashTable是一个线程安全的类,它使用s......