From 9472ca8713291b638ddd31ee1a0b73c1c04c9a81 Mon Sep 17 00:00:00 2001 From: Yihui Xiong Date: Wed, 28 Mar 2018 11:39:39 +0800 Subject: [PATCH] add mpv player option --- avs/mpv_player.py | 86 +++++++++++++++++++++++++++++++++++++++++++++++ avs/player.py | 4 ++- 2 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 avs/mpv_player.py diff --git a/avs/mpv_player.py b/avs/mpv_player.py new file mode 100644 index 0000000..9b00f29 --- /dev/null +++ b/avs/mpv_player.py @@ -0,0 +1,86 @@ +# -*- coding: utf-8 -*- + +"""Player using MPV""" + +import os +import signal +import threading +import subprocess + + +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) + t.daemon = True + t.start() + + def _run(self): + while True: + self.event.wait() + self.event.clear() + print('Playing {}'.format(self.audio)) + + master, slave = os.openpty() + self.process = subprocess.Popen(['mpv', '--no-video', 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): + self.audio = uri + self.event.set() + + if self.process and self.process.poll() == None: + os.write(self.tty, 'q') + + self.state = 'PLAYING' + + print('set play event') + + def stop(self): + if self.process and self.process.poll() == None: + os.write(self.tty, 'q') + self.state = 'NULL' + + def pause(self): + if self.state == 'PLAYING': + self.state = 'PAUSED' + os.write(self.tty, ' ') + + print('pause()') + + def resume(self): + if self.state == 'PAUSED': + self.state = 'PLAYING' + os.write(self.tty, ' ') + + # name: {eos, ...} + def add_callback(self, name, callback): + if not callable(callback): + return + + self.callbacks[name] = callback + + def on_eos(self): + self.state = 'NULL' + if 'eos' in self.callbacks: + self.callbacks['eos']() + + @property + def duration(self): + return 0 + + @property + def position(self): + return 0 diff --git a/avs/player.py b/avs/player.py index 6802d7c..4a278b6 100644 --- a/avs/player.py +++ b/avs/player.py @@ -6,7 +6,9 @@ player_option = os.getenv('PLAYER', 'default').lower() -if player_option.find('mpg123') >= 0: +if player_option.find('mpv') >= 0: + from mpv_player import Player +elif player_option.find('mpg123') >= 0: from mpg123_player import Player elif player_option.find('single') >= 0: from single_gstreamer_player import Player