Add standalone player running ability.

This commit is contained in:
Matthew Stratford 2020-10-29 21:23:37 +00:00
parent 517f32773d
commit 5076bb2e36
No known key found for this signature in database
GPG key ID: 5F50E4308A3416E8
2 changed files with 106 additions and 40 deletions

7
.vscode/launch.json vendored
View file

@ -1,6 +1,13 @@
{ {
"version": "0.2.0", "version": "0.2.0",
"configurations": [ "configurations": [
{
"name": "Python: Launch Player Standalone",
"type": "python",
"request": "launch",
"program": "./player.py",
"console": "integratedTerminal"
},
{ {
"name": "Python: Launch Server Standalone", "name": "Python: Launch Server Standalone",
"type": "python", "type": "python",

139
player.py
View file

@ -1,12 +1,13 @@
from queue import Empty
import multiprocessing
import setproctitle
import copy
import json
import time
from pygame import mixer
from state_manager import StateManager from state_manager import StateManager
from mutagen.mp3 import MP3 from mutagen.mp3 import MP3
from pygame import mixer
import time
import json
import copy
import os import os
import setproctitle
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide" os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
@ -33,9 +34,14 @@ class Player():
else: else:
return True return True
@property
def isPlaying(self): def isPlaying(self):
return bool(mixer.music.get_busy()) return bool(mixer.music.get_busy())
@property
def isLoaded(self):
return False
def play(self): def play(self):
mixer.music.play(0) mixer.music.play(0)
@ -83,7 +89,7 @@ class Player():
return "OK" return "OK"
def updateState(self, pos=None): def updateState(self, pos=None):
self.state.update("playing", self.isPlaying()) self.state.update("playing", self.isPlaying)
if (pos): if (pos):
self.state.update("pos", max(0, pos)) self.state.update("pos", max(0, pos))
else: else:
@ -108,49 +114,102 @@ class Player():
print("Setting output to: " + loaded_state["output"]) print("Setting output to: " + loaded_state["output"])
self.output(loaded_state["output"]) self.output(loaded_state["output"])
else: else:
print("Using default output device.")
self.output() self.output()
if loaded_state["filename"]: if loaded_state["filename"]:
print("Loading filename: " + loaded_state["filename"]) print("Loading filename: " + loaded_state["filename"])
self.load(loaded_state["filename"]) self.load(loaded_state["filename"])
if loaded_state["pos"] != 0: if loaded_state["pos"] != 0:
print("Seeking to pos: " + str(loaded_state["pos"])) print("Seeking to pos: " + str(loaded_state["pos"]))
self.seek(loaded_state["pos"]) self.seek(loaded_state["pos"])
if loaded_state["playing"] == True: if loaded_state["playing"] == True:
print("Resuming.") print("Resuming.")
self.unpause() self.unpause()
else:
print("No file was previously loaded.")
while self.running: while self.running:
time.sleep(0.01) time.sleep(0.01)
incoming_msg = in_q.get() try:
if (not incoming_msg): try:
continue incoming_msg = in_q.get_nowait()
if self.isInit(): except Empty:
self.updateState() # The incomming message queue was empty,
if (incoming_msg == 'PLAY'): # skip message processing
self.play() pass
if (incoming_msg == 'PAUSE'): else:
self.pause() # We got a message.
if (incoming_msg == 'UNPAUSE'): if self.isInit():
self.unpause() self.updateState()
if (incoming_msg == 'STOP'):
self.stop()
if (incoming_msg == 'QUIT'):
self.quit()
self.running = False
if (incoming_msg.startswith("SEEK")):
split = incoming_msg.split(":")
self.seek(float(split[1]))
if (incoming_msg.startswith("LOAD")):
split = incoming_msg.split(":")
self.load(split[1])
if (incoming_msg == 'DETAILS'):
out_q.put(self.getDetails())
if (incoming_msg.startswith("OUTPUT")): if (incoming_msg == 'LOADED?'):
split = incoming_msg.split(":") out_q.put(self.isLoaded)
out_q.put(self.output(split[1])) continue
if (incoming_msg == 'PLAY'):
self.play()
if (incoming_msg == 'PAUSE'):
self.pause()
if (incoming_msg == 'UNPAUSE'):
self.unpause()
if (incoming_msg == 'STOP'):
self.stop()
if (incoming_msg == 'QUIT'):
self.quit()
self.running = False
if (incoming_msg.startswith("SEEK")):
split = incoming_msg.split(":")
self.seek(float(split[1]))
if (incoming_msg.startswith("LOAD")):
split = incoming_msg.split(":")
self.load(split[1])
if (incoming_msg == 'DETAILS'):
out_q.put(self.getDetails())
if (incoming_msg.startswith("OUTPUT")):
split = incoming_msg.split(":")
out_q.put(self.output(split[1]))
# Catch the player being killed externally.
except KeyboardInterrupt:
break
except SystemExit:
break
except:
raise
print("Quiting player ", channel) print("Quiting player ", channel)
self.quit()
def showOutput(in_q, out_q):
print("Starting showOutput().")
while True:
time.sleep(0.01)
incoming_msg = out_q.get()
print(incoming_msg)
if __name__ == "__main__":
in_q = multiprocessing.Queue()
out_q = multiprocessing.Queue()
outputProcess = multiprocessing.Process(
target=showOutput,
args=(in_q, out_q),
).start()
playerProcess = multiprocessing.Process(
target=Player,
args=(-1, in_q, out_q),
).start()
# Do some testing
print("Entering infinite loop.")
while True:
pass