Skip to content

Commit

Permalink
use mpg123 terminal control instead of os signal
Browse files Browse the repository at this point in the history
  • Loading branch information
xiongyihui committed Mar 28, 2018
1 parent a1c2c32 commit 2f3547c
Showing 1 changed file with 10 additions and 24 deletions.
34 changes: 10 additions & 24 deletions avs/mpg123_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,13 @@
import subprocess


def popen(cmd, on_finished):
p = subprocess.Popen(cmd)

def run(process, on_finished):
process.wait()
on_finished()

threading.Thread(target=run, args=(p, on_finished)).start()

return p


class Player(object):
def __init__(self):
self.callbacks = {}
self.process = None
self.state = 'NULL'
self.audio = None
self.tty = None

self.event = threading.Event()
t = threading.Thread(target=self._run)
Expand All @@ -37,50 +26,47 @@ def _run(self):
self.event.wait()
self.event.clear()
print('Playing {}'.format(self.audio))
self.process = subprocess.Popen(['mpg123', self.audio])

master, slave = os.openpty()
self.process = subprocess.Popen(['mpg123', '-C', self.audio], stdin=master)
self.tty = slave

self.process.wait()
print('Finished {}'.format(self.audio))

if not self.event.is_set():
self.on_eos()

def play(self, uri):
print('play()')


if uri.startswith('file://'):
uri = uri[7:]

self.audio = uri
self.event.set()

if self.process and self.process.poll() == None:
self.process.terminate()
if self.state == 'PAUSED':
os.kill(self.process.pid, signal.SIGCONT)
os.write(self.tty, 'q')

self.state = 'PLAYING'

print('set play event')

def stop(self):
if self.process and self.process.poll() == None:
self.process.terminate()
if self.state == 'PAUSED':
os.kill(self.process.pid, signal.SIGCONT)
os.write(self.tty, 'q')
self.state = 'NULL'

def pause(self):
if self.state == 'PLAYING':
self.state = 'PAUSED'
os.kill(self.process.pid, signal.SIGSTOP)
os.write(self.tty, 's')

print('pause()')

def resume(self):
if self.state == 'PAUSED':
self.state = 'PLAYING'
os.kill(self.process.pid, signal.SIGCONT)
os.write(self.tty, 's')

# name: {eos, ...}
def add_callback(self, name, callback):
Expand Down

0 comments on commit 2f3547c

Please sign in to comment.