用python写web网页实现音乐数据库查询_使用python实现音乐播放器代码实例

news/2024/7/3 1:12:29

内核播放模块(pygame核心)

import time

import pygame

import easygui as gui

file = r'D:\CloudMusic\G.E.M.邓紫棋,艾热 - 光年之外 (热爱版).mp3' #这里为音乐文件路径

pygame.mixer.init()

gui.msgbox("正在播放"+file)

track = pygame.mixer.music.load(file)

pygame.mixer.music.play()

time.sleep(240)

pygame.mixer.music.stop()

完美版源码:

import os

import tkinter

import tkinter.filedialog

import time

import threading

import pygame #实现音频播放

#第一步:搭建界面

root = tkinter.Tk()

root.title('封亚飞的音乐播放器v1.0')

#设置窗口大小和屏幕绝对位置

root.geometry('460x600+500+100')

#固定窗口大小,设置窗口不可拉伸

root.resizable(False,False)

folder = ''# 接收文件路径 默认为空

res=[]#

num=0

now_music = ''

#第二步:实现功能

def buttonChooseClick():

#添加文件函数

global folder

global res

#如果folder不为空,则····

if not folder:

folder = tkinter.filedialog.askdirectory() #选择目录,返回目录名

musics = [folder + '\\' + music

for music in os.listdir(folder)\

\

if music.endswith(('.mp3','.m4a','.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=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('播放')

#播放音乐函数

def play():

#初始化混响设备

if len(res):

pygame.mixer.init()

global num

while playing:

if not pygame.mixer.music.get_busy():

#随机播放

nextMusci = res[num]

print(nextMusci)

print(num)

pygame.mixer.music.load(nextMusci.encode())

#播放一次

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('playing....'+''.join(nextMusci))

else:

time.sleep(0.1)

#搜索播放函数

def bottonPlayClik():

bottonNext['state']='normal'

bottonPrev['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.music.pause()

pause_resume.set('继续')

elif pause_resume.get()=='继续':

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:

num=len(res)-2

elif num ==len(res)-1:

num-=2

else:

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='停止')

#按钮布局

bottonStop.place(x=50,y=130,width=50,height=20)

#下一首

bottonNext = tkinter.Button(root,text='下一首',command=bottonNextClik)

#按钮布局

bottonNext.place(x=50,y=100,width=50,height=20)

bottonNext['state']='disabled'

#上一首

bottonPrev = tkinter.Button(root,text='上一首',command=bottonPrevClik)

#按钮布局

bottonPrev.place(x=120,y=100,width=50,height=20)

bottonPrev['state']='disabled'

#显示内容--播放状态

musicName = tkinter.StringVar(root,value='暂时没有播放音乐...')

labelName = tkinter.Label(root,textvariable=musicName)

labelName.place(x=10,y=20,width=260,height=20)

#显示内容--音量调节

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=150,width=200)

#关闭窗口

root.protocol("WM_DELETE_WINDOW",closeWindows)

#启用消息循环:显示出上一步创建的画板对象

root.mainloop()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持乐购源码。


http://lihuaxi.xjx100.cn/news/270359.html

相关文章

js的全部替换函数replaceAll

JS替换功能函数,用正则表达式解决,js的全部替换,学习js的朋友可以参考下。 alert("abacacf".replace(a,9)); alert("abacacf".replace(/a/g,9)); 第一个运行的结果 9bacaf 这个只是替换了第一个 第二个运行的结果 9b9c9f…

为什么有些公司不让用 Lombok ?

点击上方蓝色“方志朋”,选择“设为星标”回复“666”获取独家整理的学习资料!不得不承认,Lombok 是一个很不错的 Java 库,它可以让你在少写代码的同时耍耍酷,简单的几个注解,就可以干掉一大片模板代码。但…

[002] The Perks of Being a Wallflower - 读后记

The Perks of Being a Wallflower 今天(2015年10月30日 18:26:17)读完"The Perks of Being a Wallflower". 本书290页,我是在小米pad上完成阅读的,epub格式,花费四天时间,每天至少5小时. 生词很多,就不一一列出了. 使用透析法并不强求完全的正确理解原文.强调完整的阅…

基于Python的OpenCV轮廓检测聚类

点击上方“小白学视觉”,选择加"星标"或“置顶”重磅干货,第一时间送达简介OpenCV的“findContours”功能经常被计算机视觉工程师用来检测物体。OpenCV的存在,使得我们只需要编写几行代码就可以检测轮廓(对象&#xff0…

为了杀蚊子,这位博士用树莓派DIY了一把激光枪!网友:伤到人怎么办?

点击上方“视学算法”,选择加"星标"或“置顶”重磅干货,第一时间送达来源:学术头条本文约1057字,建议阅读3分钟。本文介绍了国外博士用树莓派DIY了“高端”灭蚊激光枪。世人苦蚊子久矣。尤其在夏夜,耳边嗡嗡…

变换判断滤波器类型_7.4 低通IIR滤波器的频率变换

本文将会介绍,怎样将一个离散型低通滤波器转换为其他的选频滤波器。设计连续时间选频滤波器的传统方法都是首先设计一个频率归一化的原型低通滤波器,然后用一个袋鼠变换,从原型低通滤波器推出期望的滤波器。一般在设计离散时间选频滤波器的时…

BCH专属“谷歌地图”凸显BCH魅力

2019独角兽企业重金招聘Python工程师标准>>> 在我们的日常生活中,地图作为一种工具给我们带来了很多便利。尤其是类似于谷歌地图、百度地图这些电子地图不仅仅有地图的功能,能为我们提供路线导航,更是综合了很多功能,例…

黑科技:绕过眼睛植入幻觉,科学家成功在盲人脑海中呈现指定图像!

来源 | 学术头条(ID:SciTouTiao)头图 | CSDN付费下载自视觉中国对于全球 5000 多万盲人来说,重见光明是一个遥不可及的梦想。而为了与盲人朋友进行交互,我们发明了盲文,用各种凸起的字符集合来表达各种意思。但这种通过…