-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathpyplayer.py
184 lines (156 loc) · 4.13 KB
/
pyplayer.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env python
import os
import sys
import argparse
import pygame, time
import eyed3
from gpiozero import Button
from time import sleep
from display import Driver
user = os.getuid()
if user != 0:
print ("Please run script as root")
sys.exit()
MODE_PLAY = 100
MODE_SELECT = 101
MODE_UPDATE = 102
#pygame.mixer.music.set_endevent(MUSIC_ENDED)
class MusicPlayer:
currentFile = ""
filenames = []
currentIndex = 0
driver = None
currentVol = 1
mode = MODE_PLAY
button2 = Button(16) #top button
button5 = Button(26) #center button
button3 = Button(20) #bottom button
button4 = Button(5) #left button
button1 = Button(6) #right button
def __init__(self, videoDriver):
self.driver = videoDriver
self.button5.when_pressed = self.button5Pressed
self.button3.when_pressed = self.button3Pressed
self.button2.when_pressed = self.button2Pressed
self.button1.when_pressed = self.button1Pressed
self.button4.when_pressed = self.button4Pressed
def button5Pressed(self, button):
if self.mode == MODE_PLAY:
self.mode = MODE_SELECT
print("Pause")
self.pause()
else:
if self.mode == MODE_SELECT:
print("Resume")
self.resume()
else:
if self.mode == MODE_UPDATE:
self.stop()
self.play()
def button4Pressed(self, button):
if self.mode == MODE_PLAY:
self.playNext()
self.updateDisplay(self.filenames[self.currentIndex])
else:
self.next()
def button3Pressed(self, button):
if self.mode == MODE_PLAY:
self.increaseVolume()
else:
if self.mode == MODE_SELECT:
self.next()
self.updateDisplay(self.filenames[self.currentIndex])
self.mode = MODE_UPDATE
def button2Pressed(self, button):
if self.mode == MODE_PLAY:
self.decreaseVolume()
else:
if self.mode == MODE_SELECT:
self.previous()
self.updateDisplay(self.filenames[self.currentIndex])
self.mode = MODE_UPDATE
def button1Pressed(self, button):
if self.mode == MODE_PLAY:
self.playPrevious()
self.updateDisplay(self.filenames[self.currentIndex])
else:
self.previous()
def eventloop(self):
while(True):
if pygame.mixer.music.get_busy() == False:
if self.currentIndex < len(self.filenames):
self.playNext()
self.updateDisplay(self.filenames[self.currentIndex])
else:
print("Finished")
break
sleep(0.05)
continue
print("Finished!")
def pause(self):
pygame.mixer.music.pause()
def resume(self):
pygame.mixer.music.unpause()
self.mode = MODE_PLAY
def stop(self):
pygame.mixer.music.stop()
def increaseVolume(self):
currVol = pygame.mixer.music.get_volume()
if currVol < 1.0:
newVol = currVol + 0.01
pygame.mixer.music.set_volume(newVol)
print("Volume = %f" % newVol)
def decreaseVolume(self):
currVol = pygame.mixer.music.get_volume()
if currVol > 0:
newVol = currVol - 0.01
pygame.mixer.music.set_volume(newVol)
print("Volume = %f" % newVol)
def play(self):
self.currentFile=self.filenames[self.currentIndex]
pygame.mixer.music.load(self.currentFile)
pygame.mixer.music.play(0)
self.mode = MODE_PLAY
def next(self):
if self.currentIndex < len(self.filenames):
self.currentIndex += 1
else:
self.currentIndex = 0
def playNext(self):
self.next()
self.play()
def previous(self):
if self.currentIndex > 0:
self.currentIndex -= 1
else:
self.currentIndex = len(self.filenames) - 1
def playPrevious(self):
self.previous()
self.play()
def updateDisplay(self, filename):
audiofile = eyed3.load(filename)
lines = [];
lines.append(audiofile.tag.title)
lines.append(audiofile.tag.artist)
lines.append(audiofile.tag.album)
self.driver.write_lines(lines, 14)
def index(self):
count = 0
self.driver.write_lines(["Indexing..."], 16)
for folder, subs, files in os.walk("/home/pi/Music"):
for filename in files:
musicFile = os.path.join(folder, filename)
self.filenames.append(musicFile)
print(musicFile)
count += 1
self.driver.write_lines(["%d files indexed!" % count], 20)
def main():
pygame.init()
driver = Driver()
mPlayer = MusicPlayer(driver)
mPlayer.index()
mPlayer.play()
mPlayer.updateDisplay(mPlayer.currentFile)
mPlayer.eventloop()
if __name__ == '__main__':
main()