前言
就是用Python做一个简易的音乐播放器,废话不多说,咱们直接开干
当然,今天做这个肯定不是最简单的,最简单的音乐播放器,9行代码足以
import time
import pygame
file = r'歌曲路径'
pygame.mixer.init()
print('正在播放',file)
track = pygame.mixer.music.load(file)
pygame.mixer.music.play()
time.sleep(130)
pygame.mixer.music.stop()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
开始今天的代码之前,准备工作
知识点和所需模块
- python基础知识
- requests库
- time
- pygame
- tkinter
- 线程
环境
- windows
- pycharm 2021.2
- python 3.8
完整代码,运行即可,记得???(直接明示了)
# 导入
import os
import time
import tkinter
import tkinter.filedialog
import threading
import pygame # pip
root = tkinter.Tk()
root.title('音乐播放器')
root.geometry('460x600+500+100')
root.resizable(False,False) # 不能拉伸
folder =''
res = []
num = 0
now_music = ''
def buttonChooseClick():
"""
添加文件夹
:return:
"""
global folder
global res
if not folder:
folder = tkinter.filedialog.askdirectory()
musics = [folder + '\\' + music
for music in os.listdir(folder) \
\
if music.endswith(('.mp3','.wav','.ogg'))]
ret = []
for i in musics:
ret.append(i.split('\\')[1:])
res.append(i.replace('\\','/'))
var2 = tkinter.StringVar()
var2.set(ret)
lb = tkinter.Listbox(root,listvariable=var2)
lb.place(x=50,y=100,width=260,height=300)
if not folder:
return
global playing
playing = True
# 根据情况禁用和启用相应的按钮
buttonPlay['state'] = 'normal'
buttonStop['state'] = 'normal'
# buttonPause['state'] = 'normal'
pause_resume.set('播放')
更多资料、解答加Q群:832157862
def play():
"""
播放音乐
:return:
"""
if len(res):
pygame.mixer.init()
global num
while playing:
if not pygame.mixer.music.get_busy():
netxMusic = res[num]
print(netxMusic)
print(num)
pygame.mixer.music.load(netxMusic.encode())
# 播放
pygame.mixer.music.play(1)
if len(res) -1 == num:
num = 0
else:
num = num + 1
netxMusic = netxMusic.split('\\')[1:]
musicName.set('playing......' + ''.join(netxMusic))
else:
time.sleep(0.1)
def buttonPlayClick():
"""
点击播放
:return:
"""
buttonNext['state'] = 'normal'
buttonPrev['state'] = 'normal'
# 选择要播放的音乐文件夹
if pause_resume.get() == '播放':
pause_resume.set('暂停')
global folder
if not folder:
folder = tkinter.filedialog.askdirectory()
if not folder:
return
global playing
playing = True
# 创建一个线程来播放音乐,当前主线程用来接收用户操作
t = threading.Thread(target=play)
t.start()
elif pause_resume.get() == '暂停':
# pygame.mixer.init()
pygame.mixer.music.pause()
pause_resume.set('继续')
elif pause_resume.get() == '继续':
# pygame.mixer.init()
pygame.mixer.music.unpause()
pause_resume.set('暂停')
def buttonStopClick():
"""
停止播放
:return:
"""
global playing
playing = False
pygame.mixer.music.stop()
def buttonNextClick():
"""
下一首
:return:
"""
global playing
playing = False
pygame.mixer.music.stop()
global num
if len(res) == num:
num = 0
playing = True
# 创建线程播放音乐,主线程用来接收用户操作
t = threading.Thread(target=play)
t.start()
def closeWindow():
"""
关闭窗口
:return:
"""
# 修改变量,结束线程中的循环
global playing
playing = False
time.sleep(0.3)
try:
# 停止播放,如果已停止,
# 再次停止时会抛出异常,所以放在异常处理结构中
pygame.mixer.music.stop()
pygame.mixer.quit()
except:
pass
root.destroy()
def control_voice(value=0.5):
"""
声音控制
:param value: 0.0-1.0
:return:
"""
pygame.mixer.music.set_volume(float(value))
def buttonPrevClick():
"""
上一首
:return:
"""
global playing
playing = False
pygame.mixer.music.stop()
#
# pygame.mixer.quit()
global num
# num += 1
# num -= 1
if num == 0:
num = len(res) - 2
# num -= 1
elif num == len(res) - 1:
num -= 2
else:
num -= 2
# num -= 1
print(num)
playing = True
# 创建一个线程来播放音乐,当前主线程用来接收用户操作
t = threading.Thread(target=play)
t.start()
# 窗口关闭
root.protocol('WM_DELETE_WINDOW', closeWindow)
# 添加按钮
buttonChoose = tkinter.Button(root,text='添加',command=buttonChooseClick)
# 布局
buttonChoose.place(x=50,y=10,width=50,height=20)
# 播放按钮
pause_resume = tkinter.StringVar(root,value='播放')
buttonPlay = tkinter.Button(root,textvariable=pause_resume,command=buttonPlayClick)
buttonPlay.place(x=190,y=10,width=50,height=20)
buttonPlay['state'] = 'disabled'
# 停止按钮
更多资料、解答加Q群:832157862
buttonStop = tkinter.Button(root, text='停止',command=buttonStopClick)
buttonStop.place(x=120, y=10, width=50, height=20)
buttonStop['state'] = 'disabled'
# 下一首
buttonNext = tkinter.Button(root, text='下一首',command=buttonNextClick)
buttonNext.place(x=260, y=10, width=50, height=20)
buttonNext['state'] = 'disabled'
# 上一首
buttonPrev = tkinter.Button(root, text='上一首',command=buttonPrevClick)
buttonPrev.place(x=330, y=10, width=50, height=20)
buttonPrev['state'] = 'disabled'
# 标签
musicName = tkinter.StringVar(root, value='暂时没有播放音乐...')
labelName = tkinter.Label(root, textvariable=musicName)
labelName.place(x=10, y=30, width=260, height=20)
# 音量控制
# HORIZONTAL表示为水平放置,默认为竖直,竖直为vertical
s = tkinter.Scale(root, label='音量', from_=0, to=1, orient=tkinter.HORIZONTAL,
length=240, showvalue=0, tickinterval=2, resolution=0.1,command=control_voice)
s.place(x=50, y=50, width=200)
# 显示
root.mainloop()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
效果展示
简易版的
还有个半成品的,目前代码没写完
导入模块
import os
import time
import tkinter
import tkinter.filedialog
import threading
import pygame
- 1
- 2
- 3
- 4
- 5
- 6
- 7
一、界面
root = tkinter.Tk()
root.title('音乐播放器')
root.geometry('460x600+500+100')
root.resizable(False,False) # 不能拉伸
# 显示
root.mainloop()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-H0LvDn1B-1651921570091)(https://upload-images.jianshu.io/upload_imageshttps://files.jxasp.com/image/27947649-dd5909dedb2a0bcd.png?imageMogr2/auto-orient/strip%7CimageView2https://files.jxasp.com/image/2/w/1240)]
按钮
# 添加按钮
buttonChoose = tkinter.Button(root,text='添加')
# 布局
buttonChoose.place(x=50,y=10,width=50,height=20)
# 播放按钮
pause_resume = tkinter.StringVar(root,value='播放')
buttonPlay = tkinter.Button(root,textvariable=pause_resume)
buttonPlay.place(x=190,y=10,width=50,height=20)
buttonPlay['state'] = 'disabled'
# 停止按钮
buttonStop = tkinter.Button(root, text='停止')
buttonStop.place(x=120, y=10, width=50, height=20)
buttonStop['state'] = 'disabled'
# 下一首
更多资料、解答加Q群:832157862
buttonNext = tkinter.Button(root, text='下一首')
buttonNext.place(x=260, y=10, width=50, height=20)
buttonNext['state'] = 'disabled'
# 上一首
buttonPrev = tkinter.Button(root, text='上一首')
buttonPrev.place(x=330, y=10, width=50, height=20)
buttonPrev['state'] = 'disabled'
# 标签
musicName = tkinter.StringVar(root, value='暂时没有播放音乐...')
labelName = tkinter.Label(root, textvariable=musicName)
labelName.place(x=10, y=30, width=260, height=20)
# 音量控制
# HORIZONTAL表示为水平放置,默认为竖直,竖直为vertical
s = tkinter.Scale(root, label='音量', from_=0, to=1, orient=tkinter.HORIZONTAL,
length=240, showvalue=0, tickinterval=2, resolution=0.1)
s.place(x=50, y=50, width=200)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
二、功能
# 创建一个文件目录
folder =''
res = []
num = 0
now_music = ''
# 音乐读取功能
def buttonChooseClick():
global folder
global res
if not folder:
folder = tkinter.filedialog.askdirectory()
musics = [folder + '\\' + music
for music in os.listdir(folder) \
\
if music.endswith(('.mp3','.wav','.ogg'))]
ret = []
for i in musics:
ret.append(i.split('\\')[1:])
res.append(i.replace('\\','/'))
if not folder:
return
global playing
playing = True
# 根据情况禁用和启用相应的按钮
buttonPlay['state'] = 'normal'
buttonStop['state'] = 'normal'
# buttonPause['state'] = 'normal'
pause_resume.set('播放')
# 显示已加载的音乐
var2 = tkinter.StringVar()
var2.set(ret)
lb = tkinter.Listbox(root,listvariable=var2)
lb.place(x=50,y=100,width=260,height=300)
# 播放音乐
def play():
if len(res):
pygame.mixer.init()
global num
while playing:
if not pygame.mixer.music.get_busy():
netxMusic = res[num]
print(netxMusic)
print(num)
pygame.mixer.music.load(netxMusic.encode())
# 播放
pygame.mixer.music.play(1)
if len(res) -1 == num:
num = 0
else:
num = num + 1
netxMusic = netxMusic.split('\\')[1:]
musicName.set('playing......' + ''.join(netxMusic))
else:
time.sleep(0.1)
# 点击播放
def buttonPlayClick():
buttonNext['state'] = 'normal'
buttonPrev['state'] = 'normal'
# 选择要播放的音乐文件夹
if pause_resume.get() == '播放':
pause_resume.set('暂停')
global folder
if not folder:
folder = tkinter.filedialog.askdirectory()
if not folder:
return
global playing
playing = True
# 创建一个线程来播放音乐,当前主线程用来接收用户操作
t = threading.Thread(target=play)
t.start()
elif pause_resume.get() == '暂停':
# pygame.mixer.init()
pygame.mixer.music.pause()
pause_resume.set('继续')
elif pause_resume.get() == '继续':
# pygame.mixer.init()
pygame.mixer.music.unpause()
pause_resume.set('暂停')
# 停止播放
def buttonStopClick():
global playing
playing = False
pygame.mixer.music.stop()
# 下一首
def buttonNextClick():
global playing
playing = False
pygame.mixer.music.stop()
global num
if len(res) == num:
num = 0
playing = True
# 创建线程播放音乐,主线程用来接收用户操作
t = threading.Thread(target=play)
t.start()
# 上一首
更多资料、解答加Q群:832157862
def buttonPrevClick():
global playing
playing = False
pygame.mixer.music.stop()
#
# pygame.mixer.quit()
global num
# num += 1
# num -= 1
if num == 0:
num = len(res) - 2
# num -= 1
elif num == len(res) - 1:
num -= 2
else:
num -= 2
# num -= 1
print(num)
playing = True
# 创建一个线程来播放音乐,当前主线程用来接收用户操作
t = threading.Thread(target=play)
t.start()
# 音量控制
def control_voice(value=0.5):
pygame.mixer.music.set_volume(float(value))
# 关闭窗口
def closeWindow():
# 修改变量,结束线程中的循环
global playing
playing = False
time.sleep(0.3)
try:
# 停止播放,如果已停止,
# 再次停止时会抛出异常,所以放在异常处理结构中
pygame.mixer.music.stop()
pygame.mixer.quit()
except:
pass
root.destroy()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
尾语
好了,我的这篇文章写到这里就结束啦!
有更多建议或问题可以评论区或私信我哦!一起加油努力叭(ง •_•)ง
喜欢就关注一下博主,或点赞收藏评论一下我的文章叭!!!