python实现简易云音乐播放器
作者:wanlichangkong666 时间:2021-07-02 10:13:43
本人最近在学习python,在看了一些教程后,用python写了一个简单的云音乐播放器,下面把主要代码贴上来,其中用到了github上他人写的一个汉字转拼音的库,大家可以在github上找到。
#coding=utf-8
from Tkinter import *
import tkMessageBox
import urllib
import json
import mp3play
import time
import threading
from pinyin import PinYin
import os
import stat
test = PinYin()
test.load_word()
stop=0
def music():
if not entry.get():
tkMessageBox.showinfo("温馨提示","搜索内容不能为空")
return
name = test.hanzi2pinyin_split(entry.get())
html=urllib.urlopen("http://s.music.163.com/search/get/?type=1&s=%s&limit=9"%name).read()
js=json.loads(html)
n = 0
global x
x = []
for i in js['result']['songs']:
listbox.insert(n,'%s(%s)'%(i['name'],i['artists'][0]['name']))
n+=1
x.append(i['audio'])
count = 0
#isplaying = None
def play():
global count
count += 1
index=listbox.curselection()
var1.set(u"正在加载"+listbox.get(index,last=None))
urllib.urlretrieve(x[index[0]],'tmp%s.mp3'%str(count))
var1.set(u"正在播放"+listbox.get(index,last=None))
mp3=mp3play.load("tmp%s.mp3"%str(count))
mp3.play()
time.sleep(mp3.seconds())
import inspect
import ctypes
def _async_raise(tid, exctype):
"""raises the exception, performs cleanup if needed"""
tid = ctypes.c_long(tid)
if not inspect.isclass(exctype):
exctype = type(exctype)
res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(exctype))
if res == 0:
raise ValueError("invalid thread id")
elif res != 1:
ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None)
raise SystemError("PyThreadState_SetAsyncExc failed")
def stop_thread(thread):
_async_raise(thread.ident, SystemExit)
threads=list()
t=None
def excute(event):
global t
for i in threads:
stop_thread(i)
t = threading.Thread(target=play)
t.setDaemon(True)
t.start()
threads.append(t)
root = Tk()#创建一个窗口
root.title("云音乐")
root.geometry("500x300+500+200")
entry=Entry(root)#创建输入框(单行),置父
entry.pack()
btn=Button(root,text="搜 索",command=music)
btn.pack()#布局方式必须用同一种
var=StringVar()
listbox=Listbox(root,width=50,listvariable=var)
listbox.bind('<Double-Button-1>',excute)
listbox.pack()
var1=StringVar()
label=Label(root,text="云音乐播放器",fg="purple",textvariable=var1)
var1.set("云音乐播放器")
label.pack()
root.mainloop()#显示窗口
由于最近事情较多加上我的技术还不到位,这个播放器有一个BUG我还没有解决,就是在选择播放第二首歌时,第一首歌不会停止。如果有小伙伴解决了这个BUG的话,欢迎指正。
来源:http://blog.csdn.net/wanlichangkong666/article/details/71742342
标签:python,播放器
0
投稿
猜你喜欢
ASP 高级模板引擎实现类
2011-03-25 10:54:00
python单测框架之pytest常见用法
2021-05-22 04:13:02
WinHttp.WinHttpRequest--XmlHttp获取跳转页面的功能
2010-03-22 14:34:00
PHP中使用addslashes函数转义的安全性原理分析
2023-08-16 10:31:22
如果没设置 DSN 也能建立数据库连接吗?
2009-11-02 20:16:00
Python3.6 Schedule模块定时任务(实例讲解)
2022-08-14 15:55:00
JS获取网页中HTML元素的几种方法分析
2007-10-12 13:08:00
Python实现自动驾驶训练模型
2023-07-28 18:45:29
怎么让别人看不到网页源代码
2008-03-21 13:06:00
解决sublime+python3无法输出中文的问题
2023-09-20 16:26:20
一个ASP(VBScript)简单SQL语句构建“类”
2008-03-12 07:08:00
一个简单的 js 上滚信息栏
2013-08-07 03:26:30
网站的视觉设计
2008-08-15 12:58:00
PHP getDocNamespaces()函数讲解
2023-06-13 22:19:06
ASP日期格式化函数
2010-08-08 19:18:00
Oracle SID存在解決方法
2009-06-19 17:34:00
ASPImage组件的实现过程[图]
2008-02-03 15:37:00
FSO中的SubFolders 属性介绍
2008-01-05 13:57:00
MySQL实现SQL Server的sp_executesql
2008-11-20 15:01:00
简单且有用的Python数据分析和机器学习代码
2021-03-27 13:13:27