forked from respeaker/avs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2f3547c
commit 9472ca8
Showing
2 changed files
with
89 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters