Recently, NetEase Cloud Music has caused a lot of trouble, and even been removed from major application stores. Some of its practices, Xiaobencong, also really dare not agree, but still hope that it will develop better after the rectification, of course, not only in the story-style hot comment, but also include more important copyright issues.
From this, Xiaobencong also germinated the idea of making a simple music player. First look at the renderings:
WeChat public account video demo link
The principle implementation mainly uses PyQt5, and partners familiar with PyQt5 should not be difficult to understand when reading the following program. First define the buttons of the layout interface, and then write the function implementation.
1. Code Introduction
1. Define interface elements
It is just to define the button elements used in each interface. Such as play / pause, previous / next song, loop mode, open folder, etc.
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. Select the folder where the music is stored
Just call the corresponding function of pyqt5 directly:
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('播放')
After opening the folder, display all the music files on the left side of the interface, and save some necessary information:
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. Music player
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. Music switch
There are three ways to switch music, that is, click the previous / next song:
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
Double-click a song:
1def doubleClicked(self):
2 self.slider.setValue(0)
3 self.is_switching = True
4 self.setCurPlaying()
5 self.playMusic()
6 self.is_switching = False
Set the playback mode.
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
In order to facilitate everyone's direct use, Xiaobencong finally used Python's pyinstaller package to package the source code into an exe file . You can use it directly on your computer without any Python compilation environment !
Second, the use of demo
WeChat public account video demo link
The above is the process of making this music player. WeChat public account " financial learner who learns programming " back-end " music player " to get the source code. 【Finish】
Recommended in the past
2. The Wandering Earth Movie Review
3. North Shanghai, Guangzhou and Shenzhen renting book
Your likes and attention is my greatest support!
Save the scan code and pay attention to the public number