Skip to content

Commit

Permalink
add mpv player option
Browse files Browse the repository at this point in the history
  • Loading branch information
xiongyihui committed Mar 28, 2018
1 parent 2f3547c commit 9472ca8
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
86 changes: 86 additions & 0 deletions avs/mpv_player.py
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion avs/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9472ca8

Please sign in to comment.