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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM