소스 코드:
#!/usr/bin/python3
from pygame import mixer
import time
class Player:
def __init__(self):
self.isOn = True
self.isPlaying = False
self.isPaused = False
show("MP3 Player")
mixer.init()
self.musicVolume = round(mixer.music.get_volume(), 1)
import glob
self.musicFiles = tuple(sorted(glob.glob("*.mp3")))
self.musicNum = 0
filename = self.musicFiles[self.musicNum]
show(filename + "\nReady")
def listen(self, sw_id, func, always=False):
if cad.switches[sw_id].value == 1 and (always or self.isOn):
while cad.switches[sw_id].value == 1: pass
print('self.%s()' % func)
exec('self.%s()' % func)
def playPause(self):
if self.isPlaying:
self.pause()
else:
if self.isPaused:
self.resume()
else:
self.play()
def play(self):
filename = self.musicFiles[self.musicNum]
mixer.music.load(filename)
self.musicTitle = filename
show(self.musicTitle + "\nPlaying")
mixer.music.play()
self.isPlaying = True
def pause(self):
self.isPlaying = False
mixer.music.pause()
show(self.musicTitle + "\nPaused")
self.isPaused = True
def resume(self):
self.isPlaying = True
show(self.musicTitle + "\nPlaying")
mixer.music.unpause()
self.isPaused = False
def prev(self):
self.musicNum = (self.musicNum - 1) % len(self.musicFiles)
self.play()
def next(self):
self.musicNum = (self.musicNum + 1) % len(self.musicFiles)
self.play()
def randomPlay(self):
import random
self.musicNum = random.choice(range(len(self.musicFiles)))
self.play()
def volumeUp(self):
self.setVolume("up")
def volumeDown(self):
self.setVolume("down")
def setVolume(self, action):
min, max, delta = 0.0, 1.0, 0.1
vol = mixer.music.get_volume()
vol = round(vol, 1)
if action is "up" and vol < max:
vol = round(vol + delta, 1)
mixer.music.set_volume(vol)
show(self.musicTitle + "\nvol: " + str(vol))
self.musicVolume = vol
elif action is "down" and vol > min:
vol = round(vol - delta, 1)
mixer.music.set_volume(vol)
show(self.musicTitle + "\nvol: " + str(vol))
self.musicVolume = vol
elif action is "mute":
if vol == min:
vol = self.musicVolume
mixer.music.set_volume(vol)
show(self.musicTitle + "\nvol: " + str(vol))
else:
vol = min
mixer.music.set_volume(vol)
show(self.musicTitle + "\nMuted")
else:
pass
def mute(self):
self.setVolume("mute")
def onOff(self):
if self.isOn:
if self.isPlaying:
mixer.music.stop()
show('Bye...')
time.sleep(2)
cad.lcd.backlight_off()
time.sleep(1)
cad.lcd.clear()
self.isOn = False
else:
self.__init__()
import pifacecad
cad = pifacecad.PiFaceCAD()
cad.lcd.backlight_on()
cad.lcd.cursor_off()
cad.lcd.blink_off()
def show(text):
cad.lcd.clear()
cad.lcd.write(text)
p = Player()
while True:
p.listen(0, 'playPause')
p.listen(1, 'prev')
p.listen(2, 'next')
p.listen(3, 'randomPlay')
p.listen(4, 'onOff', True)
p.listen(5, 'mute')
p.listen(6, 'volumeDown')
p.listen(7, 'volumeUp')
댓글 없음:
댓글 쓰기