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.北上广深租房图鉴

你的点赞和关注就是对我最大的支持!

保存扫码关注公众号呗

发布了11 篇原创文章 · 获赞 11 · 访问量 5721

猜你喜欢

转载自blog.csdn.net/weixin_39270299/article/details/95116894