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

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):
if self.isInit:
return bool(mixer.music.get_busy()) 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) mixer.music.play(0)
except:
return False
return True
return False
def pause(self): def pause(self):
if self.isPlaying:
try:
mixer.music.pause() mixer.music.pause()
except:
return False
return True
return False
def unpause(self): def unpause(self):
if not self.isPlaying:
try:
mixer.music.play(0, self.state.state["pos"]) mixer.music.play(0, self.state.state["pos"])
except:
return False
return True
return False
def stop(self): def stop(self):
if self.isPlaying:
try:
mixer.music.stop() mixer.music.stop()
except:
return False
return True
return False
def seek(self, pos): def seek(self, pos):
if self.isPlaying(): if self.isPlaying:
try:
mixer.music.play(0, pos) mixer.music.play(0, pos)
else: except:
self.updateState(pos) 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)
try:
mixer.music.load(filename) mixer.music.load(filename)
except:
# We couldn't load that file.
print("Couldn't load file:", filename)
return False
try:
if ".mp3" in filename: if ".mp3" in filename:
song = MP3(filename) song = MP3(filename)
self.state.update("length", song.info.length) self.state.update("length", song.info.length)
else: else:
self.state.update("length", mixer.Sound(filename).get_length()/1000) 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