diff --git a/avs/mpg123_player.py b/avs/mpg123_player.py index 0567a9a..02dcf19 100644 --- a/avs/mpg123_player.py +++ b/avs/mpg123_player.py @@ -25,25 +25,58 @@ def __init__(self): self.callbacks = {} self.process = None self.state = 'NULL' + self.audio = None + + self.event = threading.Event() + t = threading.Thread(target=self._run) + t.daemon = True + t.start() + + def _run(self): + while True: + self.event.wait() + self.event.clear() + print('Playing {}'.format(self.audio)) + self.process = subprocess.Popen(['mpg123', self.audio]) + self.process.wait() + print('Finished {}'.format(self.audio)) + + if not self.event.is_set(): + self.on_eos() def play(self, uri): - self.state = 'PLAYING' + print('play()') + + if uri.startswith('file://'): uri = uri[7:] + + self.audio = uri + self.event.set() + + if self.process and self.process.poll() == None: + if self.state == 'PAUSED': + os.kill(self.process.pid, signal.SIGCONT) + self.process.terminate() + + self.state = 'PLAYING' - print(uri) - self.process = popen(['mpg123', uri], self.on_eos) + print('set play event') def stop(self): - if self.state == 'PLAYING': - self.state = 'NULL' + if self.process and self.process.poll() == None: + if self.state == 'PAUSED': + os.kill(self.process.pid, signal.SIGCONT) self.process.terminate() + self.state = 'NULL' def pause(self): if self.state == 'PLAYING': self.state = 'PAUSED' os.kill(self.process.pid, signal.SIGSTOP) + print('pause()') + def resume(self): if self.state == 'PAUSED': self.state = 'PLAYING' @@ -57,7 +90,7 @@ def add_callback(self, name, callback): self.callbacks[name] = callback def on_eos(self): - self._state = 'NULL' + self.state = 'NULL' if 'eos' in self.callbacks: self.callbacks['eos']()