-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusicplyer.py
122 lines (97 loc) · 3.3 KB
/
musicplyer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from pygame import mixer
from tkinter import *
import tkinter.font as font
from tkinter import filedialog
root=Tk()
root.title(' music plyer ')
mixer.init()
songs_list=Listbox(root,selectmode=SINGLE,bg="black",fg="white",font=('arial',15),height=12,width=47,selectbackground="gray",selectforeground="black")
songs_list.grid(columnspan=9)
def addsongs():
temp_song=filedialog.askopenfilenames(initialdir="Music/",title="Choose a song", filetypes=(("mp3 Files","*.mp3"),))
for s in temp_song:
s=s.replace("C:/Users/DataFlair/python-mp3-music-player/","")
songs_list.insert(END,s)
def deletesong():
curr_song=songs_list.curselection()
songs_list.delete(curr_song[0])
def Play():
song=songs_list.get(ACTIVE)
song=f'C:/Users/lenovo/Desktop/DataFlair/Notepad/Music/{song}'
mixer.music.load(song)
mixer.music.play()
#to pause the song
def Pause():
mixer.music.pause()
#to stop the song
def Stop():
mixer.music.stop()
songs_list.selection_clear(ACTIVE)
#to resume the song
def Resume():
mixer.music.unpause()
#Function to navigate from the current song
def Previous():
#to get the selected song index
previous_one=songs_list.curselection()
#to get the previous song index
previous_one=previous_one[0]-1
#to get the previous song
temp2=songs_list.get(previous_one)
temp2=f'C:/Users/DataFlair/python-mp3-music-player/{temp2}'
mixer.music.load(temp2)
mixer.music.play()
songs_list.selection_clear(0,END)
#activate new song
songs_list.activate(previous_one)
#set the next song
songs_list.selection_set(previous_one)
def Next():
#to get the selected song index
next_one=songs_list.curselection()
#to get the next song index
next_one=next_one[0]+1
#to get the next song
temp=songs_list.get(next_one)
temp=f'C:/Users/DataFlair/python-mp3-music-player/{temp}'
mixer.music.load(temp)
mixer.music.play()
songs_list.selection_clear(0,END)
#activate newsong
songs_list.activate(next_one)
#set the next song
songs_list.selection_set(next_one)
#font is defined which is to be used for the button font
defined_font = font.Font(family='Helvetica')
#play button
play_button=Button(root,text="Play",width =7,command=Play)
play_button['font']=defined_font
play_button.grid(row=1,column=0)
#pause button
pause_button=Button(root,text="Pause",width =7,command=Pause)
pause_button['font']=defined_font
pause_button.grid(row=1,column=1)
#stop button
stop_button=Button(root,text="Stop",width =7,command=Stop)
stop_button['font']=defined_font
stop_button.grid(row=1,column=2)
#resume button
Resume_button=Button(root,text="Resume",width =7,command=Resume)
Resume_button['font']=defined_font
Resume_button.grid(row=1,column=3)
#previous button
previous_button=Button(root,text="Prev",width =7,command=Previous)
previous_button['font']=defined_font
previous_button.grid(row=1,column=4)
#nextbutton
next_button=Button(root,text="Next",width =7,command=Next)
next_button['font']=defined_font
next_button.grid(row=1,column=5)
#menu
my_menu=Menu(root)
root.config(menu=my_menu)
add_song_menu=Menu(my_menu)
my_menu.add_cascade(label="Menu",menu=add_song_menu)
add_song_menu.add_command(label="Add songs",command=addsongs)
add_song_menu.add_command(label="Delete song",command=deletesong)
mainloop()