天天看點

Python實用工具,pyqt5子產品,Python實作簡易版音樂播放器

前言:

利用Python制作一款簡易音樂播放器,讓我們愉快地開始吧~

Python實用工具,pyqt5子產品,Python實作簡易版音樂播放器

開發工具

Python版本:3.6.4

相關子產品:

pyqt5子產品;

以及一些Python自帶的子產品。

環境搭建

安裝Python并添加到環境變量,pip安裝需要的相關子產品即可。

原理簡介

其實相關檔案中的源代碼我已經做了一些注釋,會pyqt5的話基本看下源碼就懂了,因為原理還是很簡單的。這裡就簡單介紹一下吧。

一.設計界面

QAQ界面設計的比較簡約,大概長這個樣子:

Python實用工具,pyqt5子產品,Python實作簡易版音樂播放器

源代碼裡一個個地定義界面包含的元素,然後排版一下就行了:

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小工具系列,下篇文章分享利用郵件遠端控制自己電腦

繼續閱讀