Python implementation Music player

Source: Internet
Author: User
Tags exception handling

Development environment:
Windows10x64
python3.6
Installed packages:
Tkinter (Display interface)
Pygame (play Music)
#- -Coding:utf-8--
Import OS
Import Tkinter
Import Tkinter.filedialog
Import time
Import threading
Import Pygame

root = Tkinter. Tk ()
Root.title (' Music player v1.0 ')
#窗口大小及位置
Root.geometry (' 460x600+500+100 ')

Root.resizable (False, False)
folder = '
res = []
num = 0
Now_music = ' '
#添加文件函数
Def buttonchooseclick ():
"" Select the Music folder you want to play ""

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 ')]
#res = musics
#print (RES)
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

"" Disable and enable the corresponding button as appropriate ""

buttonplay[' state ' = ' normal '

buttonstop[' state ' = ' normal '

#buttonPause [' state '] = ' normal '

Pause_resume.set (' play ')
#播放音乐函数
Def play ():
"" Initialize Mixer Device ""
If Len (res):
Pygame.mixer.init ()
Global num
While playing:
If not pygame.mixer.music.get_busy ():

Nextmusic = Res[num]
Print (Nextmusic)
Print (num)
Pygame.mixer.music.load (Nextmusic.encode ())
"" Play Once ""
Pygame.mixer.music.play (1)
#print (Len (res)-1)
If Len (res)-1 = = num:
num = 0
Else
num = num + 1
Nextmusic = nextmusic.split (' \ ') [1:]
Musicname.set (' playing .... ' + '. Join (Nextmusic))
Else
Time.sleep (0.1)
#点击播放
Def buttonplayclick ():
buttonnext[' state ' = ' normal '

buttonprev[' state ' = ' normal '
"" Select the Music folder you want to play ""
If pause_resume.get () = = ' Play ':
Pause_resume.set (' pause ')
Global folder

If not folder:
folder = Tkinter.filedialog.askdirectory ()

If not folder:
Return

Global playing

playing = True

"" Creates a thread to play music, and the current main thread is used to receive user actions ""

t = Threading. Thread (Target=play)

T.start ()

Elif pause_resume.get () = = ' Pause ':

Pygame.mixer.music.pause ()

Pause_resume.set (' Continue ')

Elif pause_resume.get () = = ' Continue ':

Pygame.mixer.music.unpause ()

Pause_resume.set (' pause ')

#停止播放
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

"" Creates a thread to play the music, and the current main thread is used to receive the user action ""

t = Threading. Thread (Target=play)

T.start ()
#关闭窗口
Def closewindow ():
"" "Modify the variable to end the loop in the thread" "

Global playing

playing = False

Time.sleep (0.3)

Try

"" "Stop playback, if stopped,

An exception is thrown when stopped again, so it is placed in the exception handling structure "" "

Pygame.mixer.music.stop ()

Pygame.mixer.quit ()

Except

Pass

Root.destroy ()

#声音控制
def control_voice (value=0.5):
"" Sets the volume of the background music. Values from 0.0 to 1.0. Before the new music is loaded, it takes effect when the music is loaded.
Note Music is Loaded "" "
Pygame.mixer.music.set_volume (float (value))

#上一首
Def buttonprevclick ():
Global playing

playing = False

Pygame.mixer.music.stop ()

Global num

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

"" Creates a thread to play music, and the current main thread is used to receive user actions ""

t = Threading. Thread (Target=play)

T.start ()

Root.protocol (' Wm_delete_window ', CloseWindow)

Buttonchoose = Tkinter. Button (root,text= ' Add ', Command=buttonchooseclick)

Buttonchoose.place (x=50, y=10, width=50, height=20)

#print (RES)
Pause_resume = Tkinter. Stringvar (Root, value= ' play ')
Buttonplay = Tkinter. Button (Root,textvariable=pause_resume,command=buttonplayclick)

Buttonplay.place (x=190, y=10, width=50, height=20)
buttonplay[' state '] = ' disabled '
#停止播放
Buttonstop = Tkinter. Button (root,text= ' Stop ', Command=buttonstopclick)

Buttonstop.place (x=120, y=10, width=50, height=20)

buttonstop[' state '] = ' disabled '

#暂停/Continue

#下一首
Buttonnext = Tkinter. Button (root,text= ' next song ', Command=buttonnextclick)

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

buttonnext[' state '] = ' disabled '
#上一首
Buttonprev = Tkinter. Button (root,text= ' Last song ', Command=buttonprevclick)

Buttonprev.place (x=330, y=10, width=50, height=20)

buttonprev[' state '] = ' disabled '

Musicname = Tkinter. Stringvar (root,value= ' temporarily not playing music ... ')

LabelName = Tkinter. Label (Root,textvariable=musicname)

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

#HORIZONTAL表示为水平放置, default is vertical, vertical is vertical
s = tkinter. Scale (Root, label= ' volume ', 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 ()

As follows:

Python implementation Music player

Related Article

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.