Python製作音樂播放器,幫你隨時放飛心情~

最近網易雲音樂鬧出不少事情,甚至被各大應用商店下架。它的某些做法小笨聰也着實不敢苟同,但還是希望它整改後能夠發展更好,當然不只是在故事式熱評方面,還包括更爲重要的版權問題。

由此,小笨聰也萌發了製作一個簡易音樂播放器的想法。先看一下效果圖:

微信公衆號視頻演示鏈接

原理實現主要用到了 PyQt5,熟悉 PyQt5 的夥伴閱讀下面的程序應該不難看懂。首先定義佈局界面的各個按鈕,然後就是功能實現的編寫。

一、代碼介紹

1.定義界面元素

就是定義一個個界面上用到的按鈕元素就好啦。比如播放/暫停、上一首/下一首、循環模式、打開文件夾等等。

 1def __initialize(self):
 2    self.setWindowTitle('音樂播放器-學編程的金融客(小笨聰)')
 3    self.setWindowIcon(QIcon('icon.ico'))
 4    self.songs_list = []
 5    self.song_formats = ['mp3', 'm4a', 'flac', 'wav', 'ogg']
 6    self.settingfilename = 'setting.ini'
 7    self.player = QMediaPlayer()
 8    self.cur_path = os.path.abspath(os.path.dirname(__file__))
 9    self.cur_playing_song = ''
10    self.is_switching = False
11    self.is_pause = True
12    # 界面元素
13    # 播放時間
14    self.label1 = QLabel('00:00')
15    self.label1.setStyle(QStyleFactory.create('Fusion'))
16    self.label2 = QLabel('00:00')
17    self.label2.setStyle(QStyleFactory.create('Fusion'))
18    # 滑動條
19    # 播放按鈕
20    # 上一首按鈕
21    # 下一首按鈕
22    # 打開文件夾按鈕
23    # 顯示音樂列表
24    # 播放模式
25    # 計時器
26    # 界面佈局
27    self.grid = QGridLayout()
28    self.setLayout(self.grid)
29    self.grid.addWidget(self.next_button, 1, 11, 1, 2)
30    self.grid.addWidget(self.preview_button, 2, 11, 1, 2)
31    self.grid.addWidget(self.cmb, 3, 11, 1, 2)
32    self.grid.addWidget(self.open_button, 4, 11, 1, 2)

2.選取存放音樂的文件夾

直接調pyqt5相應的函數就行:

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

打開文件夾後把所有的音樂文件顯示在界面左側,並保存一些必要的信息:

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

3.音樂播放

 1# 設置當前播放的音樂
 2def setCurPlaying(self):
 3    self.cur_playing_song = self.songs_list[self.qlist.currentRow()][-1]        self.player.setMedia(QMediaContent(QUrl(self.cur_playing_song)))
 4# 播放音樂
 5def playMusic(self):
 6    if self.qlist.count() == 0:
 7        self.Tips('當前路徑內無可播放的音樂文件')
 8        return
 9    if not self.player.isAudioAvailable():
10        self.setCurPlaying()
11    if self.is_pause or self.is_switching:
12        self.player.play()
13        self.is_pause = False
14        self.play_button.setText('暫停')
15    elif (not self.is_pause) and (not self.is_switching):
16        self.player.pause()
17        self.is_pause = True
18        self.play_button.setText('播放')

4.音樂切換

音樂切換有三種方式,即點擊上一首/下一首:

 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

雙擊某首歌曲:

1def doubleClicked(self):
2    self.slider.setValue(0)
3    self.is_switching = True
4    self.setCurPlaying()
5    self.playMusic()
6    self.is_switching = False

設置播放模式。

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

 

爲了方便大家直接使用,小笨聰最後利用 Python 的 pyinstaller 包將源碼打包成 exe 文件,大家無需任何 Python 編譯環境就可以直接在電腦上使用

 

二、使用演示

微信公衆號視頻演示鏈接

 

以上就是本次音樂播放器製作的過程,微信公衆號“學編程的金融客後臺回覆“ 音樂播放器”即可獲得源碼。【完】

往期推薦

1.大學排行榜

2.流浪地球影評

3.北上廣深租房圖鑑

 

你的點贊和關注就是對我最大的支持!

保存掃碼關注公衆號唄

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章