playback.py (971B)
1 from python_mpv_jsonipc import MPV 2 import tempfile 3 4 5 class Player: 6 def __init__(self): 7 self.mpv = MPV(ipc_socket='/tmp/subclient-mpv-socket', 8 audio_display='no') 9 10 def play(self, stream, callback): 11 with tempfile.NamedTemporaryFile() as fp: 12 while True: 13 chunk = stream.read(512*1024) 14 if not chunk: 15 break 16 fp.write(chunk) 17 fp.seek(0) 18 self.mpv.play(fp.name) 19 self.mpv.wait_for_property('eof-reached') 20 self.mpv.bind_event('end-file', callback) 21 22 def exit(self): 23 self.mpv.terminate() 24 25 def is_paused(self): 26 return self.mpv.pause 27 28 def is_idle(self): 29 return self.mpv.core_idle 30 31 def set_pause(self, state): 32 self.mpv.pause = state 33 34 def stop(self): 35 self.mpv.command('stop') 36 37 def seek(self, duration='+5'): 38 self.mpv.command('seek', duration)