A program downloaded and played using the MP3 URL download address in the database

Source: Internet
Author: User
Tags pack

# _*_ Coding:utf-8 _*_
From Tkinter Import *
Import Tkmessagebox
Import JSON
Import Mp3play
Import re
Import OS
Import Sys
Import Urllib2
Import Pygame
Import time
Import Pymysql


Reload (SYS) #数据库汉子检索
Sys.setdefaultencoding (' UTF8 ')

Conn=pymysql.connect (host= ' 127.0.0.1 ', user= ' root ', passwd= ' ******* ', db= ' data ', charset= "UTF8") #连接数据库
Cur=conn.cursor () #创建光标
def music ():
Text = Entry.get ()
Text = Text.encode (' Utf-8 ')
If not text:
Tkmessagebox.showinfo (' Warm tip ', ' You can enter the following to search \n1. Song name \n2. Singer's name \n3. Part lyrics ')
Return
Else
sql = ("select * from Music where song_name=%s")
Cur.execute (SQL, text)
line = Cur.fetchall ()
If line== ():
B= "There are no songs in the database you're looking for."
A= "Please re-enter ..."

Listbox.delete (0, listbox.size ())
Listbox.insert (END, B + "..." + a)
Else
For row on line:
NAME=ROW[1]
URL=ROW[2]
Data=urllib2.urlopen (URL). Read ()
Path = "C:\users\administrator\music/songs"
If not Os.path.isdir (path):
Os.mkdir (PATH)
f = Open (path + '/' +name + '. mp3 ', ' WB ')
F.write (data) #写入本地文件
F.close ()
Listbox.delete (0, listbox.size ())
Listbox.insert (end,name + "URL address" +url)
Def play (): #播放功能
Text = Entry.get ()
Text = Text.encode (' Utf-8 ')
sound_file= (' C:\users\administrator\music\songs/%s.mp3 '%text)
Mixer=pygame.mixer
Mixer.init ()
Track = Pygame.mixer.music.load (Sound_file)
Pygame.mixer.music.play (Loops=-1)
def stop (): #停止功能
Pygame.mixer.music.stop ()
root = Tk ()
Root.title ("Music Search")
Root.geometry (' +300+100 ')
Entry = entry (root)
Entry.pack ()
Button = button (root, text= ' search song ', command=music)
Button.pack ()
Button = button (root, text= ' play song ', Command=play)
Button.pack ()
Button = button (root, text= ' pause ', command=stop)
Button.pack ()
ListBox = ListBox (Root, width=100)
Listbox.pack ()
Mainloop ()

A program that is downloaded and played using the MP3 URL in the database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.