python⾳乐编程_基于python实现⾳乐播放器代码实例核⼼播放模块(pygame内核)
import time
import pygame
python中bind函数import easygui as gui
file = r'D:\CloudMusic\G.E.M.邓紫棋,艾热 - 光年之外 (热爱版).mp3' #这⾥为⾳乐⽂件路径
pygame.mixer.init()
gui.msgbox("正在播放"+file)
track = pygame.mixer.music.load(file)
python基础代码实例pygame.mixer.music.play()
time.sleep(240)
pygame.mixer.music.stop()
完整版源代码:
import os
import tkinter
import tkinter.filedialog
import timesql语言中的update语句
import threading
import pygame #实现⾳频播放
#第⼀步:搭建界⾯
root = tkinter.Tk()
root.title('封亚飞的⾳乐播放器v1.0')
#设置窗⼝⼤⼩和屏幕绝对位置
#固定窗⼝⼤⼩,设置窗⼝不可拉伸
folder = ''# 接收⽂件路径 默认为空
res=[]#
num=0
now_music = ''
#第⼆步:实现功能
def buttonChooseClick():
global res
#如果folder不为空,则····
if not folder:
folder = tkinter.filedialog.askdirectory() #选择⽬录,返回⽬录名musics = [folder + '\\' + music
for music in os.listdir(folder)\
\
dswith(('.mp3','.m4a','.wav','.ogg'))]
ret = []
for i in musics:
ret.append(i.split('\\')[1:])
res.place("\\",'/'))
var2 = tkinter.StringVar()
var2.set(ret)
lb = tkinter.Listbox(root,listvariable=var2)
lb.place(x=50,y=220,width=260,height=300)
if not folder:
return
global playing
playing = True
#根据情况禁⽤或启⽤相应按钮
bottonPlay['state'] = 'normal'
bottonStop['state'] = 'normal'
#buttonPause['state'] = 'normal'
pause_resume.set('播放')
array在医学上什么意思#播放⾳乐函数
def play():
#初始化混响设备
if len(res):
pygame.mixer.init()
基于debian的轻量linuxglobal num
while playing:
nextMusci = res[num]
print(nextMusci)
print(num)
pygame.mixer.music.de())
#播放⼀次
pygame.mixer.music.play(1)
#print(len(res)-1)
if len(res) - 1 ==num:
num=0
else:
num +=1
nextMusci = nextMusci.split("\\")[1:]
musicName.set(''+''.join(nextMusci))
else:
time.sleep(0.1)
#点击播放函数
def bottonPlayClik():
bottonNext['state']='normal'
bottonPrev['state']='normal'
#选择要播放的⾳乐⽂件夹
if () == '播放':
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)
pygame.mixer.music.pause() pause_resume.set('继续')
elif ()=='继续': pygame.mixer.music.unpause() pause_resume.set('暂停')
#停⽌播放函数
def bottonStopClik():
global playing
playing = False
pygame.mixer.music.stop()
#下⼀⾸函数
def bottonNextClik():
global playing
playing = False
pygame.mixer.music.stop()
global num
if len(res) == num:
num=0
playing = True
global t
t = threading.Thread(target=play) t.start()
#上⼀⾸函数
def bottonPrevClik():
global playing
playing=False
pygame.mixer.music.stop()
global num
if num==0:
weigh是可数名词吗num=len(res)-2
elif num ==len(res)-1:
num-=2
print(num)
playing = True
global t
t.threading.Thread(target=play)
t.start()
#关闭窗⼝函数
def closeWindows():
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):
pygame.mixer.music.set_volume(float(value))
#添加按钮
bottonChoose=tkinter.Button(root,text='添加',command=buttonChooseClick)
#按钮布局
bottonChoose.place(x=50,y=50,width=50,height=20)
#播放按钮跟踪变量值的变化
pause_resume=tkinter.StringVar(root,value='播放')
bottonPlay=tkinter.Button(root,textvariable=pause_resume,command=bottonPlayClik) #按钮布局
bottonPlay.place(x=120,y=50,width=50,height=20)
bottonPlay['state'] = 'disabled'#未添加⽂件(刚启动)时禁⽤
#停⽌播放
bottonStop = tkinter.Button(root,text='停⽌')
#按钮布局
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论