Beef up error handling for Player

This commit is contained in:
Matthew Stratford 2020-10-29 22:25:17 +00:00
parent d34c822667
commit beba7e5806
No known key found for this signature in database
GPG key ID: 5F50E4308A3416E8

114
player.py
View file

@ -19,6 +19,7 @@ class Player():
"filename": "", "filename": "",
"channel": -1, "channel": -1,
"playing": False, "playing": False,
"loaded": False,
"pos": 0, "pos": 0,
"remaining": 0, "remaining": 0,
"length": 0, "length": 0,
@ -26,50 +27,117 @@ class Player():
"output": None "output": None
} }
@property
def isInit(self): def isInit(self):
try: try:
mixer.music.get_busy() mixer.music.get_busy()
except: except:
return False return False
else:
return True return True
@property @property
def isPlaying(self): def isPlaying(self):
return bool(mixer.music.get_busy()) if self.isInit:
return bool(mixer.music.get_busy())
return False
@property @property
def isLoaded(self): def isLoaded(self):
if not self.state.state["filename"]:
return False
if self.isPlaying:
return True
try:
current_pos = mixer.music.get_pos()
mixer.music.set_pos(current_pos)
except:
# TODO: Trigger specially off the SDLError (couldn't find it)
return False
return False return False
def play(self): def play(self):
if not self.isPlaying:
try:
mixer.music.play(0)
except:
return False
mixer.music.play(0) return True
return False
def pause(self): def pause(self):
mixer.music.pause() if self.isPlaying:
try:
mixer.music.pause()
except:
return False
return True
return False
def unpause(self): def unpause(self):
mixer.music.play(0, self.state.state["pos"]) if not self.isPlaying:
try:
mixer.music.play(0, self.state.state["pos"])
except:
return False
return True
return False
def stop(self): def stop(self):
mixer.music.stop() if self.isPlaying:
try:
mixer.music.stop()
except:
return False
return True
return False
def seek(self, pos): def seek(self, pos):
if self.isPlaying(): if self.isPlaying:
mixer.music.play(0, pos) try:
else: mixer.music.play(0, pos)
self.updateState(pos) except:
return False
return True
return False
def load(self, filename): def load(self, filename):
if not self.isPlaying(): if not self.isPlaying:
self.state.update("filename", filename) self.state.update("filename", filename)
mixer.music.load(filename)
if ".mp3" in filename: try:
song = MP3(filename) mixer.music.load(filename)
self.state.update("length", song.info.length) except:
else: # We couldn't load that file.
self.state.update("length", mixer.Sound(filename).get_length()/1000) print("Couldn't load file:", filename)
return False
try:
if ".mp3" in filename:
song = MP3(filename)
self.state.update("length", song.info.length)
else:
self.state.update("length", mixer.Sound(filename).get_length()/1000)
except:
return False
return True
def unload(self):
if not self.isPlaying:
try:
mixer.music.unload()
self.state.update("filename", "")
except:
return False
return not self.isLoaded
def quit(self): def quit(self):
mixer.quit() mixer.quit()
@ -90,6 +158,7 @@ class Player():
def updateState(self, pos=None): def updateState(self, pos=None):
self.state.update("playing", self.isPlaying) self.state.update("playing", self.isPlaying)
self.state.update("loaded", self.isLoaded)
if (pos): if (pos):
self.state.update("pos", max(0, pos)) self.state.update("pos", max(0, pos))
else: else:
@ -142,7 +211,8 @@ class Player():
pass pass
else: else:
# We got a message. # We got a message.
if self.isInit(): if self.isInit:
self.updateState() self.updateState()
if (incoming_msg == 'LOADED?'): if (incoming_msg == 'LOADED?'):
@ -209,7 +279,11 @@ if __name__ == "__main__":
).start() ).start()
# Do some testing # Do some testing
in_q.put("LOADED?")
in_q.put("PLAY")
in_q.put("LOAD:\\Users\\matth\\Documents\\GitHub\\bapsicle\\dev\\test.mp3")
in_q.put("LOADED?")
in_q.put("PLAY")
print("Entering infinite loop.") print("Entering infinite loop.")
while True: while True:
pass pass